Declare XML namespaces in classes imported by WSDL (Jax-Ws)

Asked

Viewed 259 times

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 exchanges. But when creating the object and passing as parameter I receive the message from the server:

undeclared namespace prefix 'x' at offset 143 of http://urlexample.com

When I marshalling the object to check the XML 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>
   ...

but 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 should I make this namespace statement in the XML header if all classes were generated when I imported the WSDL?

  • Post the code on which you mount this XML

  • @Marquezani was just that?

  • In this snippet you generate pure XML without the SOAP envelope. Do you have the code that generates the SOAP envelope? Post it please

1 answer

0

In the archive package-info.java (generated on WSDL import), changes 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 little 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.