我想使用 spring-ws 实现我自己的 SOAP webservcie。我写了一个 xsd,从中生成一个 Java 类。
这是我的网络服务配置:
@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet, "/ws/*");
}
@Bean(name = "foo")
public DefaultWsdl11Definition defaultWsdl11DefinitionFoo(){
DefaultWsdl11Definition defaultWsdl11Definition = new DefaultWsdl11Definition();
defaultWsdl11Definition.setPortTypeName("FooPort");
defaultWsdl11Definition.setLocationUri("/ws");
defaultWsdl11Definition.setTargetNamespace("foo");
defaultWsdl11Definition.setSchema(fooSchema());
return defaultWsdl11Definition;
}
@Bean
public XsdSchema fooSchema() {
return new SimpleXsdSchema(new ClassPathResource("foo.xsd"));
}
这是我的 xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="foo" elementFormDefault="qualified">
<xs:complexType name="Foobject">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="population" type="xs:int"/>
</xs:sequence>
</xs:complexType>
这是我的端点:
@Endpoint
public class Fooendpoint {
private static final String NAMESPACE_URI = "foo";
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "bar")
@ResponsePayload
public Foobject getCountry(@RequestPayload Foobject request) {
return new Foobject("string", 0);
}}
最后,这是 Web 服务在 http://localhost:8080/ws/foo.wsdl
处发布的自动生成的 WSDL
<wsdl:definitions targetNamespace="foo" xmlns:sch="foo" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="foo" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<xs:schema elementFormDefault="qualified" targetNamespace="foo" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Foobject">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="population" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:portType name="FooPort"/>
<wsdl:binding name="FooPortSoap11" type="tns:FooPort">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
</wsdl:binding>
<wsdl:service name="FooPortService">
<wsdl:port binding="tns:FooPortSoap11" name="FooPortSoap11">
<soap:address location="http://localhost:8080/ws"/>
</wsdl:port>
</wsdl:service>
在 WSDL 中没有消息和操作。我错过了什么?
更新:我修改了XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="foo" elementFormDefault="qualified" xmlns:tns="foo">
<xs:element name="fooRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="foo" type="tns:Foobject"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="fooResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="string" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Foobject">
<xs:sequence>
<xs:element name="string" type="xs:string"/>
<xs:element name="int" type="xs:int"/>
</xs:sequence>
</xs:complexType>
WSDL 现在具有消息和绑定:
<wsdl:definitions targetNamespace="foo" xmlns:sch="foo" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="foo" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<xs:schema elementFormDefault="qualified" targetNamespace="foo" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="fooRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="foo" type="tns:Foobject"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="fooResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="string" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Foobject">
<xs:sequence>
<xs:element name="string" type="xs:string"/>
<xs:element name="int" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="fooResponse">
<wsdl:part element="tns:fooResponse" name="fooResponse"/>
</wsdl:message>
<wsdl:message name="fooRequest">
<wsdl:part element="tns:fooRequest" name="fooRequest"/>
</wsdl:message>
<wsdl:portType name="FooPort">
<wsdl:operation name="foo">
<wsdl:input message="tns:fooRequest" name="fooRequest"/>
<wsdl:output message="tns:fooResponse" name="fooResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="FooPortSoap11" type="tns:FooPort">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="foo">
<soap:operation soapAction=""/>
<wsdl:input name="fooRequest">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="fooResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="FooPortService">
<wsdl:port binding="tns:FooPortSoap11" name="FooPortSoap11">
<soap:address location="http://localhost:8080/ws"/>
</wsdl:port>
</wsdl:service>
但是当我通过 SoapUI 发送请求时,找不到端点:
No endpoint mapping found for [SaajSoapMessage {foo}fooRequest]
对于任何可能需要"解决方案"的人来说,这里有一些(真正的(技巧可以解决这个问题。问题出在春季的参数匹配算法内部。它使用"org.w3c.Element的参数实例"进行检查。如果不是,则没有匹配项,并且不会调用该方法。
顺便说一句:如果有人有更好的解决方案,请告诉我!它一定在某个地方。
因此,以防万一,您得到:
// No adapter for endpoint [public <CorrectType> <CorrectMethod>(<CorrectArgumentType>)]: Is your endpoint annotated with @Endpoint, or does it implement a supported interface like MessageHandler or PayloadEndpoint?
您可以将方法更改为:
public org.w3c.Element correctMethod(org.w3c.Element parameter) {...
现在调用该方法,您可以取消封送参数并使用 JAXBContext 封送结果。
对于封送处理,您可以使用如下方法:
private <T> T unmarshalParameter(Element element, Class<? extends T> targetClass) {
JAXBContext context = null;
try {
context = JAXBContext.newInstance(targetClass);
return context.createUnmarshaller().unmarshal(element, targetClass).getValue();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
结果的封送处理可以像这样完成:
private <T> Element marshal(T response, String elementName) {
JAXBContext jaxbContext = null;
try {
Document document =
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
jaxbContext = JAXBContext.newInstance(response.getClass());
DOMResult res = new DOMResult(document);
JAXBElement<?> jaxbElement =
new JAXBElement<>( new QName(NAMESPACE_URI,elementName),
(Class<T>)response.getClass(),
response);
jaxbContext.createMarshaller().marshal(jaxbElement,res);
Element e = (Element)document.getDocumentElement();
return e;
} catch (JAXBException | ParserConfigurationException e) {
throw new RuntimeException(e);
}
}