简体   繁体   English

CXF,输出拦截器,更改响应SOAP正文

[英]CXF, out interceptor, alter response SOAP body

Using CXF interceptor, I want to alter the response body which is being sent to the client as response. 使用CXF拦截器,我想更改作为响应发送给客户端的响应主体。 (I want to add some more nodes) My interceptor is configured as an outgoing interceptor, is being invoked but the body is empty. (我想添加更多节点)我的拦截器配置为传出拦截器,正在被调用,但主体为空。

public class AlterOutBodyInterceptor extends AbstractSoapInterceptor {
    private SAAJOutInterceptor saajOut = new SAAJOutInterceptor();

    public AlterOutBodyInterceptor() {
        super(Phase.PRE_MARSHAL);
        getBefore().add(SAAJOutInterceptor.class.getName());
    }

    public void handleMessage(SoapMessage message) throws Fault {
        SOAPMessage doc = message.getContent(SOAPMessage.class);
        if (doc == null) {
            saajOut.handleMessage(message);
            doc = message.getContent(SOAPMessage.class);
            // it is executed
        }

        try {
            SOAPBody body = doc.getSOAPBody();
            // here, body doesn't contain anything that the client gets
        } catch (SOAPException e) {
            e.printStackTrace();
        }
    }
}

I am doing trial-error with getBefore - getAfter and Phases, but without luck. 我正在使用getBefore-getAfter和Phases进行试错,但是没有运气。 Thanks in advance! 提前致谢!

The solution is as here: (we checked cxf sources because it contains a lot of interceptors, we got the ideas from there) 解决方案如下:(我们检查了cxf源,因为它包含很多拦截器,我们从那里得到了建议)

public class Interceptor extends AbstractPhaseInterceptor<Message> {
    private ObjectFactory objectFactory = new ObjectFactory();

    public Interceptor() {
        super(Phase.PRE_LOGICAL);
        addBefore(HolderOutInterceptor.class.getName());
    }

    @Override
    public void handleMessage(Message message) {
        MessageContentsList outObjects = MessageContentsList.getContentsList(message);
        Assert.isTrue(outObjects.size() == 1);
        Object outObject = outObjects.get(0);
        // object is our soap response
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM