2

如何在 JAX-WS 中更改 SOAP 请求前缀。我在句柄消息中更新了 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" 

请帮忙

4

2 回答 2

3

我需要将默认前缀从 S 更改为 soapenv。这是我所做的:

  1. 创建设置前缀的 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;
    }
    
  2. 执行以下操作之一:

    • 创建一个用作 HandlerResolver 的 XML 文件(请参阅更改 JAX-WS 默认 XML 命名空间前缀),然后使用 @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()); 
      
于 2013-11-19T11:06:42.843 回答
3
    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();
于 2013-03-18T20:29:50.840 回答