ExceptionMapper 未使用 jersey2 和 jboss 7.11 调用
ExceptionMapper not called with jersey2 and jboss 7.11
我的 jaxrs 项目中有一个异常映射器来捕获所有 NotFoundException 并格式化响应。当运行 in 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();
}
}
jersey 从 2.7 版移至 2.24 版,现在调用异常映射器。
我的 jaxrs 项目中有一个异常映射器来捕获所有 NotFoundException 并格式化响应。当运行 in 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();
}
}
jersey 从 2.7 版移至 2.24 版,现在调用异常映射器。