没有使用jersey2和jboss 7.11调用ExceptionMapper

时间:2017-05-04 15:33:58

标签: jboss jersey jax-rs jersey-2.0

我的jaxrs项目中有一个异常映射器来捕获所有NotFoundException并格式化响应。在Tomcat中运行时,抛出异常时将调用异常映射器。我现在不得不转移到JBoss,一切正常,除了不再调用异常映射器。

Web.xml中

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>

<servlet>
    <servlet-name>RestConfigServer</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>com.xxx.rest.resources.RestConfigServerResourceImpl</param-value>
    </init-param>

    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.xxx.rest.exceptions</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
</servlet>

<listener>
    <listener-class>com.xxx.ContextListener</listener-class>
</listener>

<servlet-mapping>
    <servlet-name>RestConfigServer</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<!--  For JBoss as it ships with RESTEasy JAX-RS as a implicit dependency -->
<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan.providers</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan.resources</param-name>
    <param-value>false</param-value>
</context-param>

异常映射器

import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import com.openet.modules.config.server.rest.response.ErrorInfo;
import com.openet.modules.config.server.rest.response.ErrorResponse;

@Provider
public class NotFoundExceptionMapper implements  ExceptionMapper<NotFoundException> {
    public Response toResponse(NotFoundException ex) {
        ex.printStackTrace();
        return Response
            .status(ex.getResponse().getStatus())
            .entity(new ErrorResponse(new ErrorInfo(ex)))
            .type(MediaType.APPLICATION_JSON)
            .build();
    }
}

1 个答案:

答案 0 :(得分:0)

从版本2.7移动到2.24的泽西,现在调用异常映射器。