java-如何在未启用日志记录功能的情况下在Out Interceptor中获取http响应正文?

我已使用CXF 3.0.0 Milestone1创建了Rest服务,并且试图在SEND阶段的Out Interceptor中获取HTTP响应的正文,并将其放入String变量中,而未在xml配置文件中启用日志记录功能.

这个问题与我前一段时间问的问题“ How do I get the response payload of a REST service from the Message of an outgoing Interceptor?”非常相似.区别在于,在我的旧问题中,我启用了Apache CXF的日志记录功能

<jaxrs:features>
    <cxf:logging/>
</jaxrs:features>

当我删除< cxf:logging />从我的xml文件中,然后得到ClassCastException.

这是我的拦截器代码:

public void handleMessage(Message message) throws Fault {
    BAprintHouse.setFunction("MyLoginOutInterceptor.handleMessage()");

    OutputStream os = message.getContent(OutputStream.class);
    StringBuilder responsePayload = new StringBuilder();
    CachedOutputStream cos = (CachedOutputStream) os;
    Exchange ex = message.getExchange();
    try {
        cos.writeCacheTo(responsePayload);
    } catch (IOException e) {
        BAprintHouse.log("ERROR", "IO Error:" + e.getMessage());
    }
}

异常发生在CachedOutputStream cos =(CachedOutputStream)os行中;

java.lang.ClassCastException: org.apache.cxf.transport.http.AbstractHTTPDestination$WrappedOutputStream cannot be cast to org.apache.cxf.io.CachedOutputStream
    at com.test.interceptors.MyLoginOutInterceptor.handleMessage(MyLoginOutInterceptor.java:35)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
    at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:77)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
    at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223)
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203)
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
    at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:158)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:243)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:168)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:219)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:722)

有什么方法可以从消息中获取响应主体,并且无论是否使用< cxf:logging />到位?

谢谢

解决方法:

CXF LoggingOutInterceptor(由日志记录功能启用)创建CachedOutputStream(作为CacheAndWriteOutputStream),这就是为什么要获得ClassCastException的原因.它从未创建.如果消息中不存在,则需要编写一个拦截器来创建CachedOutputStream.例如:

public class CacheOutputInterceptor extends AbstractPhaseInterceptor<Message> {

    public CacheOutputInterceptor() {
        super(Phase.PRE_STREAM);
        addAfter(LoggingOutInterceptor.class.getName());
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        final OutputStream os = message.getContent(OutputStream.class);

        if (os != null && !(os instanceof CachedOutputStream) ) {
            final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(os);
            message.setContent(OutputStream.class, newOut);
        }

    }

}

您还应该查看org.apache.cxf.interceptor.LoggingOutInterceptor.handleMessage(Message)的源,如果启用了日志记录功能,则在该源中创建CacheAndWriteOutputStream.

上一篇:微信小程序--登陆页面


下一篇:小程序开发-7-访问api数据与ES6在小程序中的应用