Changing the default XML namespace prefix generated with JAXWS

前端 未结 2 573
悲&欢浪女
悲&欢浪女 2020-11-29 07:30

I am using JAXWS to generate a WebService client for a Java Application we\'re building.

When JAXWS build its XMLs to use in SOAP protocol, it generates the followin

2条回答
  •  天涯浪人
    2020-11-29 08:27

    Maybe it's late for you and I'm not sure if this may work, but you can try.

    First you need to implement a SoapHandler and, in the handleMessage method you can modify the SOAPMessage. I'm not sure if you can modify directly that prefix but you can try:

    public class MySoapHandler implements SOAPHandler
    {
    
      @Override
      public boolean handleMessage(SOAPMessageContext soapMessageContext)
      {
        try
        {
          SOAPMessage message = soapMessageContext.getMessage();
          // I haven't tested this
          message.getSOAPHeader().setPrefix("soapenv");
          soapMessageContext.setMessage(message);
        }
        catch (SOAPException e)
        {
          // Handle exception
        }
    
        return true;
      }
    
      ...
    }
    

    Then you need to create a HandlerResolver:

    public class MyHandlerResolver implements HandlerResolver
    {
      @Override
      public List getHandlerChain(PortInfo portInfo)
      {
        List handlerChain = Lists.newArrayList();
        Handler soapHandler = new MySoapHandler();
        String bindingID = portInfo.getBindingID();
    
        if (bindingID.equals("http://schemas.xmlsoap.org/wsdl/soap/http"))
        {
          handlerChain.add(soapHandler);
        }
        else if (bindingID.equals("http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/"))
        {
          handlerChain.add(soapHandler);
        }
    
        return handlerChain;
      }
    }
    

    And finally you'll have to add your HandlerResolver to your client service:

    Service service = Service.create(wsdlLoc, serviceName);
    service.setHandlerResolver(new MyHandlerResolver());
    

提交回复
热议问题