CXF,拦截器,改变响应 SOAP 主体

CXF, out interceptor, alter response SOAP body

使用 CXF 拦截器,我想更改作为响应发送给客户端的响应主体。 (我想添加更多节点) 我的拦截器配置为传出拦截器,正在调用,但是主体是空的。

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

我正在使用 getBefore - getAfter 和 Phases 进行试错,但没有成功。提前致谢!

解决方法如下: (我们检查了 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
    }
}