简体   繁体   中英

SOAP without namespace in response (Spring Boot)

I'm following this example to build a web service with Soap. Is there a way to remove the targetNamespace for the Soap response? With SoapUI I get this response:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
 <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
   <ns2:getAllResponse xmlns:ns2="http://spring.io/guides/gs-producing-web-service">
    <ns2:timestamp>12/18/2015 10:51:02 AM</ns2:timestamp>
    <ns2:MyItem>
       <ns2:class>myClass</ns2:class>
       <ns2:name>MyItemName</ns2:name>
    </ns2:MyItem>
  </ns2:getAllResponse>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

And would like to get it without the target namespace, eg like:

...    
<MyItem>
<MyItem>
       <class>myClass</class>
       <name>MyItemName</name>
</MyItem>

I'm sending an empty request to get a list of MyItems. My xsd looks like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
xmlns:tns="http://spring.io/guides/gs-producing-web-service"     
targetNamespace="http://spring.io/guides/gs-producing-web-service">

 <xs:element name="getAllRequest">
    <xs:complexType>
        <xs:sequence>
        </xs:sequence>
    </xs:complexType>
 </xs:element>

 <xs:element name="getAllResponse">
    <xs:complexType>
        <xs:sequence>
            <xs:element maxOccurs="1" name="timestamp" type="xs:string"/>
            <xs:element maxOccurs="unbounded" name="MyItem" type="tns:MyItem"/>
        </xs:sequence>
    </xs:complexType>
 </xs:element>   

 <xs:complexType name="MyItem">
    <xs:sequence>
        <xs:element name="class" type="xs:string"/>
        <xs:element name="name" type="xs:string"/>
    </xs:sequence>
 </xs:complexType>
</xs:schema>

The MyItem class will be generated automatically and gets annotated with this:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MyItem", propOrder = {
   "class",
   "name",
})
public class MyItem { //....

Is there any way to get a response without the target namespace? I know its purpose and that it's actually quite useful. But it happened that it can't be processed any further by another tool with this target namespace and looks like this:

<MyItem xmlns="http://spring.io/guides/gs-producing-web-service"> ...

Thanks in advance!

Is there any way to get a response without the target namespace?

I hope not. A SOAP web service should have a well-defined, published interface and every response that it sends should conform to that interface. Usually, the interface is described by a WSDL document.

And would like to get it without the target namespace

<MyItem>
   <class>myClass</class>
   <name>MyItemName</name>
</MyItem>

Being pedantic, but the absence of a namespace prefix does not mean that there is no namespace. There might be a default namespace in force. So you could achieve XML that looks exactly like the above and the MyItem, class and name tags might still have a non-empty namespace.

I know its purpose and that it's actually quite useful.

The purpose of a namespace in an XML document ( whether a web service response, or any other XML ) is to avoid polluting the global namespace. Do you really want 'name' and 'class' to have exactly one meaning in your applications? Or would it be better if each schema could define its own version of those?

But it happened that it can't be processed any further by another tool with this target namespace and looks like this:

<MyItem xmlns="http://spring.io/guides/gs-producing-web-service"> ...

That looks like valid XML, so what's the problem? Is that output produced by the 'other tool'? Or is it something that the 'other tool' cannot handle?

Here is the simple and easiest solution for that problem. Create Package-Info.Java file in your model package and add the below script to that.

@javax.xml.bind.annotation.XmlSchema(namespace = "http://spring.io/guides/gs-producing-web-service", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, xmlns = { @javax.xml.bind.annotation.XmlNs(namespaceURI = "http://spring.io/guides/gs-producing-web-service", prefix = "") })
package my.com.scicom.stars.model;

And add elementFormDefault as "qualified" in your xsd or wsdl file.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://spring.io/guides/gs-producing-web-service"
           targetNamespace="http://spring.io/guides/gs-producing-web-service"
           elementFormDefault="qualified">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM