我将创建 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/