2014-09-23 80 views
1

我在想,如果這樣的事情是可能的,或者是否有更好的方法來做到這一點:的Apache CXF ExceptionMapper與請求消息

public class WebServiceExceptionMapper implements ExceptionMapper<Exception> { 

    private static Logger logger = LoggerFactory.getLogger(WebServiceExceptionMapper.class); 

    @Override 
    public Response toResponse(Exception ex, Message requestMessage) { 
    logger.error("request=" + httpMessage.etc..etc.., ex); 
    return Response.status(Status.INTERNAL_SERVER_ERROR).build(); 
    } 

} 

基本上,我想能夠登錄不只是例外,但請求觸發錯誤的消息。更具體地說,如果請求包含JSON或XML,我想抓住它。

回答

0

NVM。通過在我的ExceptionMapper中注入MessageContext並添加如此處所述的自定義RequestHandler來找出它:http://cxf.547215.n5.nabble.com/Accessing-JAXRS-JSON-content-directly-td4390185.html

public class StringContentRequestHandler implements RequestHandler { 

private static final Logger logger = LoggerFactory.getLogger(RawContentRequestHandler.class); 

public Response handleRequest(Message m, ClassResourceInfo resourceClass) { 
    InputStream is = m.getContent(InputStream.class); 
    try { 
     CachedOutputStream bos = new CachedOutputStream(); 
     IOUtils.copy(is, bos); 

     bos.flush(); 
     is.close(); 

     m.setContent(InputStream.class, bos.getInputStream()); 
     StringBuilder builder = new StringBuilder(); 
     bos.writeCacheTo(builder, "utf-8"); 
     m.setContent(String.class, builder.toString()); 
     return null; 

    } catch (IOException ex) { 
     logger.error("IOException on getting raw content", ex); 
     return null; 
    } 
} 
}