简体   繁体   English

没有命名空间的 SOAP 响应(Spring Boot)

[英]SOAP without namespace in response (Spring Boot)

I'm following this example to build a web service with Soap.我正在按照此示例使用 Soap 构建 Web 服务。 Is there a way to remove the targetNamespace for the Soap response?有没有办法删除 Soap 响应的 targetNamespace? With SoapUI I get this response:使用 SoapUI 我得到这样的回应:

<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.我正在发送一个空请求以获取 MyItems 列表。 My xsd looks like this:我的 xsd 看起来像这样:

<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: MyItem 类将自动生成并使用以下注释:

@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. SOAP Web 服务应该有一个定义良好的、已发布的接口,并且它发送的每个响应都应该符合该接口。 Usually, the interface is described by a WSDL document.通常,接口由 WSDL 文档描述。

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.因此,您可以实现与上述完全一样的 XML,并且 MyItem、类和名称标签可能仍然具有非空的命名空间。

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. XML 文档中的命名空间(无论是 Web 服务响应还是任何其他 XML)的目的是避免污染全局命名空间。 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?这看起来像是有效的 XML,那么有什么问题呢? 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.在您的模型包中创建 Package-Info.Java 文件并将以下脚本添加到该文件中。

@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.并在您的 xsd 或 wsdl 文件中将 elementFormDefault 添加为“合格”。

<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">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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