如何在JAX-WS中更改SOAP请求前缀。我更新了handlemessage中的setprofix方法
SOAPMessage msgs = ctx.getMessage();
SOAPMessage sm = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
sm.getSOAPPart().getEnvelope().setPrefix("soap");
sm.getSOAPPart().getEnvelope().removeNamespaceDeclaration("env");
sm.getSOAPHeader().setPrefix("soap");
sm.getSOAPBody().setPrefix("soap");*/
但我仍然得到相同的请求
<?xml version="1.0"?>
<S:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
我需要
<Soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
请帮忙
答案 0 :(得分:3)
我需要将默认前缀从S更改为soapenv。这是我做的:
创建SOAPHandler的实现,它设置前缀。
public class SoapNamespaceHandler implements SOAPHandler<SOAPMessageContext>{
private final static String SOAP_PREFIX = "soapenv";
@Override
public boolean handleMessage(final SOAPMessageContext context){
//only update the namespace prefix for outbound messages (requests)
final Boolean isSoapRequest = (Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (isSoapRequest){
try{
//get the soap message and envelope
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope env = soapMsg.getSOAPPart().getEnvelope();
//set the prefix
env.setPrefix(SOAP_PREFIX);
// **** apply the changes to the message
soapMsg.saveChanges();
}
catch (SOAPException e) {
e.printStackTrace();
}
}
return true;
}
执行以下操作之一:
创建一个充当HandlerResolver的XML文件(请参阅Changing JAX-WS default XML namespace prefix),然后使用@HandlerChain(file =“handler.xml”)注释您的Web服务客户端类
<?xml version="1.0" encoding="UTF-8"?>
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
<handler-chain>
<handler>
<handler-name>mypackage.SoapNamespaceHandler</handler-name>
<handler-class>mypackage.SoapNamespaceHandler</handler-class>
</handler>
</handler-chain>
</handler-chains>
创建HandlerResolver的实现......
public class SoapNamespaceHandlerResolver implements HandlerResolver {
@SuppressWarnings({ "rawtypes" })
@Override
public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> handlerChain = new ArrayList<Handler>();
Handler handler = (SOAPHandler<SOAPMessageContext>) new SoapNamespaceHandler();
String bindingID = portInfo.getBindingID();
if (bindingID.equals("http://schemas.xmlsoap.org/wsdl/soap/http")) {
handlerChain.add(handler);
} else if (bindingID.equals("http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/")) {
handlerChain.add(handler);
}
return handlerChain;
}
}
...然后通过调用
以编程方式将HandlerResolver实现附加到您的Web服务客户端 webServiceClient.setHandlerResolver(new SoapNamespaceHandlerResolver());
答案 1 :(得分:2)
final SOAPMessage soapMsg = context.getMessage();
soapMsg.getSOAPPart().getEnvelope().setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/");
soapMsg.getSOAPPart().getEnvelope().removeAttributeNS("http://schemas.xmlsoap.org/soap/envelope/", "env");
soapMsg.getSOAPPart().getEnvelope().removeAttribute("xmlns:env");
soapMsg.getSOAPPart().getEnvelope().setPrefix("soap");
soapMsg.getSOAPBody().setPrefix("soap");
soapMsg.getSOAPPart().getEnvelope().getHeader().detachNode();