2012-10-12 44 views
1

在我目前安裝一個彈簧CXF的JAX-RS的Servlet,異常被記錄到log4j的DEBUG道:如何讓CXF將異常記錄到ERROR通道?

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等更相關的東西?

回答

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通道。

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; 
} 

以下內容添加到您的春季檔

<bean id="exceptionResponseBuilder" class="com.orgname.project.provider.ExceptionResponseBuilder"> 
    <property name="printStackTrace" value="true" /> 
</bean> 
     <jaxrs:providers> 
     <ref bean="exceptionResponseBuilder"/> 
    </jaxrs:providers>