Java SpringWS端点文档文字包装/展开
我正在与一个期望我们使用WS-I 1.1文档/文字包装消息的系统集成。我们有一个可行的解决方案,但似乎可以有一个更简单的方法来处理有效负载包装/展开 我有一个端点,如下所示:Java SpringWS端点文档文字包装/展开,java,spring,soap,wsdl,spring-ws,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") @Respon
@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);
}
}
@Endpoint
公共类FooEndpoint
{
@自动连线
私人餐饮服务;
@PayloadRoot(localPart=“Foo”,命名空间=”http://foo/service")
@回应书
公共图书馆(
@RequestPayload(JAXBElement请求元素)
{
FooRequestType请求=requestElement.getValue();
FooAcknowledgementType响应=FooService.Foo(请求);
//TODO:使用包装响应找到更好的解决方案
返回新的JAXBElement(新的QName(
"http://foo/service“,“食品确认”),
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>
FooWeb服务
我已经从上面端点类中引用的foo_types.xsd模式生成了jaxb对象
问题是,当我们以文档/文字包装的方式接收soap消息时,soap主体负载会是这样的
<x:Foo>
<!-- elements from s:FooRequestType -->
</x:Foo>
我们期望用soap主体负载来响应,比如
<x:FooAcknowledgement>
<!-- elements from s:FooAcknowledgementType -->
</x:FooAcknowledgement>
SpringWS是否可以开箱即用地处理这个问题?我们可以按原样使用代码来消费和生成兼容的消息,但这似乎不是正确的方法,因为spring文档中没有引用这种样式,我们的解决方案是通过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>
不,不太可能。您可以使用ObjectFactory
使其更干净一点。问题是JAXB对象不是@XmlRootElement
s,因此不能直接使用,必须进行包装。您可以更改xsd,但这意味着您需要控制xsd,如果您不需要的话,您可以做的不多。我使用了JAXB绑定文件将这些注释添加到JAXB对象中,所以这不是原因。这不仅仅是这些注释可用的事实,在没有正确的xsd结构的情况下,仅仅在上面添加注释是没有帮助的。xsd是一个稳定的接口,许多其他实现者正在使用它,所以不要认为它的结构不正确。我认为带JAXB的SpringWS似乎不会长期使用文字包装样式。如果JAXB无法确定根元素,那么仅仅添加@XmlRootElement
是没有帮助的。JAXB需要能够基于XSD确定它们。这与SpringWS无关,而是一个JAXB问题,它不知道如何封送对象,因此需要对它们进行包装。您可以尝试使用Jaxb2Marshaller
并将supportJaxbElementClass
设置为true,然后使用您的类重试(非包装)。