【问题标题】:Custom ExceptionMapper not working in Spring + Jersey + Jetty自定义 ExceptionMapper 在 Spring + Jersey + Jetty 中不起作用
【发布时间】:2015-02-01 23:04:58
【问题描述】:

我的异常映射器如下所示

@Provider
public class DAExceptionMapper implements ExceptionMapper<DAException> {
    @Override
    public @ResponseBody Response toResponse(DAException exception) {
    }
}

public class DAException extends Exception {
}

我在 Jetty 8.1.14 上使用 Spring 3.2.5 和 Jersey 2.15。

我找不到为什么我的异常映射器没有在 Spring 中注册。当我在我的应用程序中抛出一个自定义 DAException 时,我得到 500。我尝试了https://java.net/jira/browse/JERSEY-2063 中的所有解决方案。他们都没有工作。

我目前没有在 spring 上下文 xml 中注册的任何异常 bean。我的应用程序没有通过程序设置资源。

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is DAException [responseStatus=Unauthorized, errorMessage=ErrorMessage [errorMessage=User is not allowed to access this resource.], errorDescription=User is not allowed to access this resource.]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)

【问题讨论】:

  • 尝试使DAException扩展WebApplicationException
  • @peeskillet,可以工作,但我真的想使用界面选项而不是扩展。

标签: java spring rest spring-mvc jersey


【解决方案1】:

你实现了 toResponse 方法吗?

@Provider
public class DAExceptionMapper implements
        ExceptionMapper<DAException> {
    @Override
    public Response toResponse(DAException ex) {
        return Response.status(404).entity(ex.getMessage()).type("text/plain")
                .build();
    }
}

还有这个,但听起来这与球衣版本有关 > 2.5 toResponse in jersey ExceptionMapper does not get invoked

【讨论】:

  • 很抱歉没有更新完整的课程。但我不得不在课堂上做出回应。另外,我尝试了您提供的页面链接中的所有解决方案。添加单例或服务注释也不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-26
  • 2017-03-21
  • 2015-05-18
  • 2011-07-13
  • 1970-01-01
  • 2018-10-23
  • 1970-01-01
相关资源
最近更新 更多