简体   繁体   中英

spring-ws SOAP — Missing Messages and Binding

I want to implement my own SOAP webservcie using spring-ws. I have written a xsd, from which a Java class is generated.

This is my webservice configuration:

@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"));
}

This is my 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>

This is my endpoint:

@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);
}}

And finally this is the autogenerated WSDL the webservice publishes at http://localhost:8080/ws/foo.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>

in the WSDL there is no messages and operations. What am I missing?

Update: I modified the 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>

The WSDL now has messages and bindings:

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

But when I send a request via SoapUI, the Endpoint is not found:

No endpoint mapping found for [SaajSoapMessage {foo}fooRequest]

For anybody may in need of a "solution" here is a little (real) hack around the problem. The problem is inside the parameter match algorithm in spring. It checks with "parameter instanceof org.w3c.Element". If not you do not have a match and the method is not called.

Btw: If anybody has a better solution please let me know. It must be somewhere.

So in case, you got a:

// 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?

You may change the method to:

public org.w3c.Element correctMethod(org.w3c.Element parameter) {...

Now the method gets called and you can unmarshal the parameter and marshal the result with a JAXBContext.

For marshaling you can use a method like this:

    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);
        }
    }

Marshaling of the result can be done like:

    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);
        }
    }

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