Java 无法在SOAPHandler中记录完整的SOAP消息
我在客户端创建了一个SOAPHandler来记录发送到服务器的请求,并在SOAP消息中添加了一个SOAP头。它记录发送的SOAP xml,而不记录头部分。但是在服务器端,我可以使用SOAP头记录整个消息 服务器端日志:Java 无法在SOAPHandler中记录完整的SOAP消息,java,web-services,soap,jax-ws,Java,Web Services,Soap,Jax Ws,我在客户端创建了一个SOAPHandler来记录发送到服务器的请求,并在SOAP消息中添加了一个SOAP头。它记录发送的SOAP xml,而不记录头部分。但是在服务器端,我可以使用SOAP头记录整个消息 服务器端日志: <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-E
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<Credential xmlns="http://soap.header.test.com" password="123" username="ashok"/>
</SOAP-ENV:Header>
<S:Body>
<ns2:addNumber xmlns:ns2="http://service.ashok.com/">
<arg0>10</arg0>
<arg1>200</arg1>
</ns2:addNumber>
</S:Body>
请建议我如何在客户端使用标题部分记录SOAP消息问题是,在消息被修改后,您正在调用writeTo,但您仍然打印旧版本(没有任何标题更改)。我建议您使用SOAPMessage中的方法 抽象void saveChanges() 使用对该SOAPMessage对象所做的所有更改更新该对象 然后尝试调用writeTo方法。让我知道这是否有效 我尝试了您的代码,如下所示:
EchoServiceImplService echoService = new EchoServiceImplService();
HandlerResolver resolver = new HandlerResolver(){
@Override
public List<Handler> getHandlerChain(PortInfo portInfo){
List<Handler> handlers = new ArrayList<Handler>();
handlers.add(new LogMessageHandler());
return handlers;
}
};
echoService.setHandlerResolver(resolver);
EchoService port = echoService.getEchoServiceImplPort();
EchoServiceImplService echoService=新的EchoServiceImplService();
HandlerResolver解析器=新HandlerResolver(){
@凌驾
公共列表getHandlerChain(PortInfo PortInfo){
列表处理程序=新的ArrayList();
add(新的LogMessageHandler());
返回处理程序;
}
};
echoService.setHandlerResolver(解析器);
EchoService端口=EchoService.getEchoServiceImplPort();
及
public类LogMessageHandler实现SOAPHandler{
@凌驾
公共布尔handleMessage(SOAPMessageContext上下文){
试一试{
if((布尔)context.get(SOAPMessageContext.MESSAGE\u出站\u属性)){
SOAPMessage=context.getMessage();
SOAPEnvelope信封=message.getSOAPPart().getEnvelope();
SOAPHeader=envelope.getHeader();
if(null==标题)
header=envelope.addHeader();
QName凭据=新的QName(“http://soap.header.test.com“,”凭证“);
SOAPHeaderElement-headerElement=header.addHeaderElement(凭证);
QName用户名=新的QName(“用户名”);
addAttribute(用户名“ashok”);
QName密码=新的QName(“密码”);
headerElement.addAttribute(密码为“123”);
消息写入(系统输出);
返回true;
}
}捕获(例外e){
System.err.println(“处理程序中发生错误”);
}
返回false;
}
我能够在客户端看到完整的SOAPMessage,只需在将请求发送到服务器之前调用处理程序
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header>
<Credential xmlns="http://soap.header.test.com" password="123" username="ashok"/></S:Header>
<S:Body>
<ns2:echo xmlns:ns2="http://echo.soap.ws.jax.koitoer.com/">
<arg0>Koitoer echo </arg0>
</ns2:echo>
</S:Body>
</S:Envelope>
科托尔回声
解决方案是在方法writeTo之后调用方法saveChanges
public boolean handleMessage(SOAPMessageContext context) {
Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) { // outbound: request to send
try {
SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.getHeader();
if (header == null)
header = envelope.addHeader();
SOAPElement soapSecurity = header.addChildElement("Security", "wsse",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
soapSecurity.setAttribute(header.getPrefix() + ":mustUnderstand", "true");
...
context.getMessage().saveChanges();
context.getMessage().writeTo(System.out);
} catch (Exception e) {
e.printStackTrace();
}
} else {
// inbound: response received
}
return true;
}
对不起我的英语
问候语。我看不到您在代码示例中使用了
saveChanges
。感谢saveChanges()有效。message.saveChanges();message.writeTo(System.out);解决方案是在writeTo(打印消息的方法)之后调用方法saveChanges。context.getMessage().saveChanges();
public class LogMessageHandler implements SOAPHandler<SOAPMessageContext> {
@Override
public boolean handleMessage(SOAPMessageContext context){
try {
if ((Boolean) context.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)) {
SOAPMessage message = context.getMessage();
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
SOAPHeader header = envelope.getHeader();
if (null == header)
header = envelope.addHeader();
QName credential = new QName("http://soap.header.test.com", "Credential");
SOAPHeaderElement headerElement = header.addHeaderElement(credential);
QName username = new QName("username");
headerElement.addAttribute(username, "ashok");
QName password = new QName("password");
headerElement.addAttribute(password, "123");
message.writeTo(System.out);
return true;
}
} catch (Exception e) {
System.err.println("An error in handler occurs.");
}
return false;
}
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header>
<Credential xmlns="http://soap.header.test.com" password="123" username="ashok"/></S:Header>
<S:Body>
<ns2:echo xmlns:ns2="http://echo.soap.ws.jax.koitoer.com/">
<arg0>Koitoer echo </arg0>
</ns2:echo>
</S:Body>
</S:Envelope>
public boolean handleMessage(SOAPMessageContext context) {
Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) { // outbound: request to send
try {
SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
SOAPHeader header = envelope.getHeader();
if (header == null)
header = envelope.addHeader();
SOAPElement soapSecurity = header.addChildElement("Security", "wsse",
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
soapSecurity.setAttribute(header.getPrefix() + ":mustUnderstand", "true");
...
context.getMessage().saveChanges();
context.getMessage().writeTo(System.out);
} catch (Exception e) {
e.printStackTrace();
}
} else {
// inbound: response received
}
return true;
}