不在Jersey项目中调用ExceptionMapper方法

时间:2016-04-02 23:26:02

标签: java tomcat jersey

我使用了@Provider注释

的自定义异常映射器
@Provider
public class DataNotFoundExceptionMapper implements ExceptionMapper<DataNotFoundException>{

    @Override
    public Response toResponse(DataNotFoundException arg0) {

        ErrorMessage errorMessage = new ErrorMessage(arg0.getMessage(), 404);
        return Response.status(Status.NOT_FOUND)
                        .entity(errorMessage)
                        .build();

    }

}

问题是尽管如此,我继续获得Tomcat服务器错误页面。这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>io.soumasish.SimpleRestApi.Resources</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
</web-app>

现在我已经在SO

上检查了这个问题

SO Question

这表明有两种解决方案,

using classes.add(EntityExceptionMapper.class); worked for me thanks a lot @Provider does not seems to work



 Adding the following line to the init
    jerseyServletFactory.addExceptionMapper(new MyCustomExceptionHandler());

有人可以帮助我了解我在哪里可以执行这两个步骤中的任何一个。任何帮助表示赞赏。

0 个答案:

没有答案