1

在我当前安装的 Spring CXF JAX-RS Servlet 中,异常被记录到 log4j DEBUG 通道:

DEBUG http-8134-2 org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper - WebApplicationException has been caught, status: 500, message: Unrecognized field "links" (Class Result), not marked as ignorable
at [Source: {"links"}; line: 1, column: 11] (through reference chain: bbc.news.naf.elections.data.model.client.Result["links"])
javax.ws.rs.WebApplicationException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "links" (Class Result), not marked as ignorable
at [Source: {"links":[{}; line: 1, column: 11] (through reference chain: bbc.news.naf.elections.data.model.client.Result["links"])
    at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:243)
    at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:89)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:211)
    at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:213)

显然,这并不理想。如何让 CXF 将这些异常堆栈跟踪记录到更相关的内容,如 WARN 或 ERROR?

4

2 回答 2

2

编写一个简单的提供者类如下

public class ExceptionResponseBuilder 
  extends WebApplicationExceptionMapper {
Logger _logger = LoggerFactory.getLogger(this.getClass().getName());

public Response toResponse(WebApplicationException ex) {

//Do something to get the object you want to return. 
// ex object will give you information you need
     Response r = Response
                .status(Response.Status.BAD_REQUEST)
                .entity(set any object your want to return)
                .build();


    return r;
}

将以下内容添加到您的 spring 文件中

    <bean id="exceptionResponseBuilder" class="com.orgname.project.provider.ExceptionResponseBuilder">
   <property name="printStackTrace" value="true" />
</bean>
        <jaxrs:providers>
        <ref bean="exceptionResponseBuilder"/>
    </jaxrs:providers>
于 2013-02-19T03:19:21.053 回答
0

您需要在 WebApplicationExceptionMapper 类上启用 printStackTrack 功能。您可以通过将以下内容添加到 Spring XML 上下文中来做到这一点:

<jaxrs:providers>
  <bean class="org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper">
    <property name="printStackTrace" value="true" />
  </bean>
</jaxrs:providers>

现在应该将异常记录到 log4j WARN 通道。

于 2012-10-16T16:12:11.453 回答