java - 在服务器请求方法中获取并保存 SOAP 传入请求 (XML) ?没有处理程序

标签 java web-services cxf jax-ws jbossfuse

我将创建 Web 服务(org.apache.cxf)并将其作为蓝图包安装到 Jboss fusion 中,这就是我的 Web 服务实现具体类的样子。

我想在服务器请求时访问 SOAP 消息并保存请求 XML(包括 header 、正文)。尽管如此,我仍无法访问 SOAP 消息。在不使用处理程序或拦截器的情况下如何实现这一目标?

我正在使用org.apache.cxf.jaxws

SOAP消息的类型为org.apache.cxf.binding.soap.SoapMessage

package ats.emvo.callback;

import java.io.InputStream;

import javax.annotation.Resource;
import javax.xml.ws.WebServiceContext;
import javax.jws.WebService;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import org.apache.cxf.jaxws.context.WrappedMessageContext;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.io.CachedOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;    

public class ProductService implements IProductService {

    private static final Logger _log = LoggerFactory.getLogger(ProductService.class);


    @Resource
    private WebServiceContext webServiceContext;

    @Override
    public void ConfirmProductMasterDataStatus() {

        try {
        if (webServiceContext != null) {

            WrappedMessageContext mc = (WrappedMessageContext) webServiceContext.getMessageContext();
            if (mc != null) {
                SoapMessage soapMessage = (SoapMessage) mc.getWrappedMessage();
                if(soapMessage != null) {

                    XMLStreamReader body  = soapMessage.getContent(XMLStreamReader.class); 

                    _log.info("soap message messConInputStream"+ body.toString());
                }else {
                    _log.info("soapMessage  is null");
                }

            }else {
                _log.info("WrappedMessageContext  is null");
            }
            }

        } catch (Exception e) {
            _log.error("Error", e);
        }

    }

最佳答案

不,您不能在服务类中执行此操作,因为在该阶段 SoapMessage 已经被消耗。 您必须在很早的阶段使用拦截器,一些代码如

public class SaveSoapInterceptor extends AbstractPhaseInterceptor<Message> {

public SaveSoapInterceptor() {
    super(Phase.RECEIVE);
    addBefore(LoggingInInterceptor.class.getName());
}

public void handleMessage(Message message) throws Fault {
    InputStream is = message.getContent(InputStream.class);
    if (is != null) {
        CachedOutputStream bos = new CachedOutputStream();
        try {
            IOUtils.copy(is, bos);

            bos.flush();
            is.close();
            message.setContent(InputStream.class, bos.getInputStream());
            bos.close();
            String soapMessage = new String(bos.getBytes());// here you get the soap message

    }

}

}

关于java - 在服务器请求方法中获取并保存 SOAP 传入请求 (XML) ?没有处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48200861/

相关文章:

java - 如何在JAVA中使用Elastic Transcoder?

java - Spring 切入点指示符差异(在 vs 执行中)

c# - 动态 C#.NET Web 服务

web-services - 如何使用 cxf 组件使用 Apache camel 调用第三方 Web 服务

java - Apache CXF |对 Web 服务调用的多个响应

java - CXF 故障拦截器 - 记录 SOAP 故障消息,如 SOAP 客户端中所示

java - 如何在自己定制的服务器中获取GPS设备数据?

java - 在 java 中使用 SOAP webservice,只有 WSDL 在手

web-services - Rest WebService 错误处理

cxf - 如何使用 CXF 隐藏 WSDL