Declaring namespaces in XML in classes imported by WSDL (jax-ws)

1

I'm developing a WebService application using jax-ws. In this model, after importing the WSDL, the IDE generates the classes that will be used in the information exchange. However when I create the object and pass as a parameter I get the message from the server:

  

undeclared namespace prefix 'x' at offset 143 of link

When I do the marshalling of the object to check the XML if it is correct I get:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ans:solicitacaoProcedimentoWS xmlns:ans="http://exemplo" 
 xmlns:ns2="http://www.w3.org/2000/09/xmldsig#">
<ans:cabecalho>
   ...
</ans:cabecalho>
   ...

However, I must inform this namespace in the header as follows:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
 xmlns:ans="http://exemplo" xmlns:xd="http://www.w3.org/2000/09/xmldsig#">
<soapenv:Header/>
<soapenv:Body>
   <ans:solicitacaoProcedimentoWS>
      ...
   </ans:solicitacaoProcedimentoWS>
      ...

Below the code that converts the object to XML:

 public static String converteBeanXML(Object bean) {
    try {
        Writer writer = new StringWriter();
        JAXBContext context = JAXBContext.newInstance(bean.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(bean, writer);

        return writer.toString();
    } catch (JAXBException ex) {
        escreveLogErro(ex.getMessage(), "SisUtil.converteBeanXML()");
        return null;
    }
}

How do I make this declaration of namespaces in the XML header if all classes were generated when I imported WSDL?

    
asked by anonymous 18.07.2017 / 21:29

1 answer

0

In the package-info.java file (generated in the WSDL import), change the @XmlSchema annotation to look like this:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ans.gov.br/padroes/tiss/schemas", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, xmlns = {@javax.xml.bind.annotation.XmlNs(prefix="sch", namespaceURI="http://www.ans.gov.br/padroes/tiss/schemas")})

If you use Apache CXF, the solution is a bit different:

final Client c = ClientProxy.getClient(port);
final Map<String, String> nsMap = new HashMap<>();
nsMap.put("sch", "http://www.ans.gov.br/padroes/tiss/schemas");
c.getRequestContext().put("soap.env.ns.map", nsMap);
    
03.04.2018 / 20:45