这是我正在尝试做的事情,我似乎遗漏了一些关于 RESTEasy 文档的内容,所以提前感谢您的帮助。
我的团队决定始终向我们的 REST 公开服务(仅 JSON)返回 200(ok)状态。因此,为此我们决定依赖 RESTEasy 框架提供的异常映射器机制。
这意味着对于我们代码中的给定(业务或技术)问题,我们会引发特殊的 RuntimeException,然后映射器必须构建一个 ErrorBean 作为响应体。
这是执行此操作的代码中最重要的部分。
引发特殊异常的资源方法:
@GET
@Produces({ MediaType.APPLICATION_JSON })
@BadgerFish
@ApiOperation(value = "gets all documents of the current user", notes = "gets all documents of the current user", responseClass = "com.cdc.pcp.api.bean.DocumentVO", multiValueResponse=true)
public Response getAllDocuments() throws PCPResourceException {
try {
UserInformation userInformation = userService.getUserInformation(getUsername());
List<ParapheurNodeInformation> nodesInformations = new ArrayList<ParapheurNodeInformation>();
nodesInformations = taskService.getTasksReadyToSignOrValidate(userInformation);
int i = 0/0;
return Response.ok(BeanConverter.toDocumentVOList(nodesInformations)).build();
} catch (Exception e) {
LOG.error("Erreur lors de la récupération de la liste des documents pour l'utilisateur : " + getUsername() , e);
throw new PCPResourceException(e.getMessage(), "Erreur lors de la récupération de la liste des documents pour l'utilisateur", ResourceErrorCode.INTERNAL_ERROR, e);
}
}
特殊异常映射器:
@Provider
public class PCPResourceExceptionMapper implements ExceptionMapper<PCPResourceException> {
@Override
public Response toResponse(PCPResourceException exception) {
ErrorBean eb =new ErrorBean();
eb.setDeveloperMessage("lol");
eb.setErrorCode(0);
eb.setGeneration(new Date());
eb.setGravity(Gravity.ERROR);
eb.setMoreInfoUrl("lolll");
eb.setUserFriendlyMessage("fuck");
GenericEntity<ErrorBean> e = new GenericEntity<ErrorBean>(eb) {
};
return Response.ok(e).build();
}
}
日志
org.jboss.resteasy.spi.UnhandledException: org.jboss.resteasy.spi.WriterException: java.lang.NullPointerException
org.jboss.resteasy.core.SynchronousDispatcher.writeFailure(SynchronousDispatcher.java:460)
org.jboss.resteasy.core.SynchronousDispatcher.executeExceptionMapper(SynchronousDispatcher.java:349)
org.jboss.resteasy.core.SynchronousDispatcher.unwrapException(SynchronousDispatcher.java:373)
org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:362)
org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:233)
org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:209)
org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:557)
org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:524)
org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126)
org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208)
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55)
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.cdc.pcp.api.util.ApiOriginFilter.doFilter(ApiOriginFilter.java:32)