Spring-WS端点记录文字包装/解包

时间:2015-06-17 04:36:31

标签: java spring soap wsdl spring-ws

我正在与希望我们使用WS-I 1.1文档/文字包装消息的系统集成。我们有一个可行的解决方案,但似乎可以有一种更简单的方法来处理有效负载包装/解包。

我的端点如下:

@Endpoint
public class FooEndpoint
{

  @Autowired
  private FooService FooService;

  @PayloadRoot(localPart = "Foo", namespace = "http://foo/service")
  @ResponsePayload
  public JAXBElement<FooAcknowledgementType> Foo(
        @RequestPayload JAXBElement<FooRequestType> requestElement)
  {
    FooRequestType request = requestElement.getValue();
    FooAcknowledgementType response = FooService.Foo(request);

    // TODO: Find a better solution with the wrapped response
    return new JAXBElement<FooAcknowledgementType>(new QName(
          "http://foo/service", "FooAcknowledgement"),
          FooAcknowledgementType.class, null, response);
  }
}

和定义合同的WSDL如下:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:tns="http://foo/service" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns="http://www.w3.org/2000/09/xmldsig#" xmlns:ns1="http://foo/schema"
  name="Foo" targetNamespace="http://foo/service">
  <wsdl:types>
    <xsd:schema xmlns:s="http://foo/schema" targetNamespace="http://foo/service">
      <xsd:import namespace="http://foo/schema" schemaLocation="foo_types.xsd" />
      <xsd:element name="Foo" type="s:FooRequestType" />
      <xsd:element name="FooAcknowledgement" type="s:FooAcknowledgementType" />
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="FooRequest">
    <wsdl:part name="parameter" element="tns:Foo" />
  </wsdl:message>
  <wsdl:message name="FooAcknowledgement">
    <wsdl:part name="parameter" element="tns:FooAcknowledgement" />
  </wsdl:message>
  <wsdl:portType name="FooPortType">
    <wsdl:operation name="Foo">
      <wsdl:input message="tns:FooRequest" />
      <wsdl:output message="tns:FooAcknowledgement" />
    </wsdl:operation>
  </wsdl:portType>

  <wsdl:binding name="FooBinding" type="tns:FooPortType">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="Foo">
      <soap:operation soapAction="http://foo/serviceFoo" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="Foo">
    <wsdl:documentation>Foo web service</wsdl:documentation>
    <wsdl:port name="FooService" binding="tns:FooBinding">
      <soap:address location="http://localhost:8080/foo/services/Foo" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

我已经从上面的端点类中引用的foo_types.xsd模式生成了jaxb对象。

问题在于,当我们接收文件/文字包装样式的肥皂消息时,肥皂体有效负载看起来像

<x:Foo>
  <!-- elements from s:FooRequestType -->
</x:Foo>

我们应该使用像

这样的肥皂体负载来回应
<x:FooAcknowledgement>
  <!-- elements from s:FooAcknowledgementType -->
</x:FooAcknowledgement>

无论如何,Spring-WS可以开箱即用吗?我们可以使用代码来使用和生成符合要求的消息,但似乎这可能不是正确的方法,因为http://docs.spring.io/spring-ws/site/reference/html/tutorial.html#tutorial.xsd的弹簧文档中未引用此样式

1 个答案:

答案 0 :(得分:0)

我们的解决方案是通过我们的jaxb bindings.xml将名称空间和元素名称添加到@XmlRootElement注释中,例如

<jaxb:bindings node="xs:complexType[@name='fooType']">
  <annox:annotate target="class">
    <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="FooAcknowledgement" namespace="http://foo/service"/>
  </annox:annotate>
</jaxb:bindings>