更改JAX-WS中的SOAPRequest前缀
问题描述:
如何在JAX-WS中更改SOAP请求前缀。在的handleMessage我更新setprofix方法更改JAX-WS中的SOAPRequest前缀
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"
请帮
答
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();
答
我需要改变从S到soap的默认前缀ENV。这是我做的:
-
创建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; }
-
执行下列操作之一:
-
创建一个XML文件,其功能如同一个HandlerResolver(见Changing JAX-WS default XML namespace prefix),然后用注释您@HandlerChain Web服务客户端类(file =“文件处理程序.XML“)
<?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; } }
...然后编程调用
webServiceClient.setHandlerResolver(new SoapNamespaceHandlerResolver());
-
你试图找到命名空间附上HandlerResolver实现你的Web服务客户端???尝试找到一个有点像setNamespace()的方法......它可能会有所帮助......为什么Soap需要而不是s,它只是一个命名空间...... – 2013-02-19 21:41:12
你的代码片段是正确的。刚刚使用Metro JAX-WS分发版进行测试2.2.1-1。您强调的JAX-WS实现可能是一个问题。你使用哪个Web服务库? – Florin 2013-02-20 15:53:36
我已经使用wsimport JDK 1.6提供的WSDL生成了代码。 build 27. – 2013-02-20 21:36:13