RESTEasy JSON异常响应:发送对象列表

时间:2014-09-13 11:55:12

标签: java json web-services exception-handling resteasy

我有基于RESTEasy webservices和会话bean的Web客户端(HTML5)和后端服务器。在我的服务器端代码中,我正在迭代对象列表和每个对象,我正在执行一些业务逻辑:

List<TestTO> failedTestList = new ArrayList<TestTO>();
for (TestTO testTO : testTOList) {
    try {
        // some weired business logic :P
    } catch (Exception e) {
        logger.error("Unable to create data -" + e.getMessage());
        failedTestList.add(testTO);
    }
}

if (!failedTestList.isEmpty()) {
   // throw custom exception embedded with failed TO list 
}

我编写了自定义异常处理程序,以捕获异常并将正确的响应返回给客户端。这个类看起来像:

public class CustomExceptionHandler implements ExceptionMapper<CustomException> {

    public CustomException getCustomErrorCode(final CustomException customException) {

        // Some logic to get cause and set error code
        return customException;
    }


    @Override
    public Response toResponse(final CustomException customException) {

        return Response.serverError().entity(
                "{\"Error Code\":\"" + getCustomErrorCode(customException).getErrorCode() + "\", "
                        + "\"Error Message\":\"" + customException.getLocalizedMessage() + "\"}").build();
    }
}

我正在考虑将此失败的TO列表发送回客户端的选项,以便它可以理解哪些对象失败的处理。我正在阅读不同的文章,但找不到符合我要求的任何内容。

请给我一个想法并链接到参考,如何实现这样的要求。请注意,我的客户希望以JSON格式进行响应。如果您需要更多信息,请告诉我。 感谢。

0 个答案:

没有答案