Spring-WS webservice with MTOM attachment - Hello world test

后端 未结 1 856
情书的邮戳
情书的邮戳 2020-12-30 12:10

I am trying to create a simple Spring webservice which when called returns a file attachment as part of the SOAP response. The Enpoint class is shown below:

And fin

相关标签:
1条回答
  • 2020-12-30 12:59

    Finally managed to get it to work. The configuration is more or less the same as what i had in my original post. I had to update the configuration file. Here is how my configuration file looks like now.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ws="http://www.springframework.org/schema/web-services"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <context:component-scan base-package="com.mypackage.ws" />
    
        <ws:annotation-driven />
    
        <ws:dynamic-wsdl id="serviceDefinition"
            portTypeName="Msm" locationUri="http://localhost:8080/MyWebService/webservice">
            <ws:xsd location="/WEB-INF/schemas/schema.xsd" />
        </ws:dynamic-wsdl>
    
        <bean id="messageReceiver"
            class="org.springframework.ws.soap.server.SoapMessageDispatcher">
            <property name="endpointAdapters">
                <list>
                    <ref bean="defaultMethodEndpointAdapter" />
                </list>
            </property>
        </bean>
    
        <bean id="defaultMethodEndpointAdapter"
            class="org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter">
            <property name="methodArgumentResolvers">
                <list>
                    <!-- Be careful here! You might need to add more processors if you do 
                        more than webservices! -->
                    <ref bean="marshallingPayloadMethodProcessor" />
                </list>
            </property>
            <property name="methodReturnValueHandlers">
                <list>
                    <ref bean="marshallingPayloadMethodProcessor" />
                </list>
            </property>
        </bean>
    
        <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="contextPath" value="com.mypackage.ws" />
            <property name="mtomEnabled" value="true" />
        </bean>
    
        <bean id="marshallingPayloadMethodProcessor"
            class="org.springframework.ws.server.endpoint.adapter.method.MarshallingPayloadMethodProcessor">
            <constructor-arg ref="marshaller" />
            <constructor-arg ref="marshaller" />
        </bean>
    </beans>
    

    The changes i added were based on what i read on this article - blog.hpxn.net/2012/06/

    Edit - An example

    Here is an example (based on the spring samples) that returns the attachment in MTOM format. I just tried it and the response is shown below:

       HTTP/1.1 200 OK
    Server: Apache-Coyote/1.1
    Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    SOAPAction: ""
    Content-Type: Multipart/Related; start-info="text/xml"; type="application/xop+xml"; boundary="----=_Part_0_17910623.1342789122256"
    Transfer-Encoding: chunked
    Date: Fri, 20 Jul 2012 12:58:42 GMT
    
    ------=_Part_0_17910623.1342789122256
    Content-Type: application/xop+xml; charset=utf-8; type="text/xml"
    
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:LoadImageResponse xmlns:ns2="http://www.springframework.org/spring-ws/samples/mtom"><ns2:name>?</ns2:name><ns2:image><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:f1c3ef65-a519-4bba-9b92-9acffc0c14f7%40www.springframework.org"/></ns2:image></ns2:LoadImageResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
    ------=_Part_0_17910623.1342789122256
    Content-Type: image/png
    Content-ID: <f1c3ef65-a519-4bba-9b92-9acffc0c14f7@www.springframework.org>
    Content-Transfer-Encoding: binary
    
    ‰PNG
    

    Note that i have not configured any Axiom factories. The necessary files are listed below:

    schema.xsd

    <?xml version="1.0" encoding="UTF-8"?>
    <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.springframework.org/spring-ws/samples/mtom"
            xmlns:tns="http://www.springframework.org/spring-ws/samples/mtom"
            xmlns:xmime="http://www.w3.org/2005/05/xmlmime" elementFormDefault="qualified">
    
        <element name="StoreImageRequest" type="tns:Image"/>
    
        <element name="LoadImageRequest" type="string"/>
    
        <element name="LoadImageResponse" type="tns:Image"/>
    
        <complexType name="Image">
            <sequence>
                <element name="name" type="string"/>
                <element name="image" type="base64Binary" xmime:expectedContentTypes="image/png"/>
            </sequence>
        </complexType>
    
    </schema>
    

    spring-ws-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    
        <bean id="imageRepository" class="org.springframework.ws.samples.mtom.service.StubImageRepository"/>
    
        <bean class="org.springframework.ws.samples.mtom.ws.ImageRepositoryEndpoint">
            <constructor-arg ref="imageRepository"/>
        </bean>
    
        <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping"/>
    
        <bean class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter">
            <constructor-arg ref="marshaller"/>
        </bean>
    
        <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="contextPath" value="org.springframework.ws.samples.mtom.schema"/>
            <property name="mtomEnabled" value="true"/>
        </bean>
    
        <bean id="mtom" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
            <property name="schema" ref="schema"/>
            <property name="portTypeName" value="ImageRepository"/>
            <property name="locationUri" value="http://localhost:8080/mtom-server/"/>
        </bean>
    
        <bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
            <property name="xsd" value="/WEB-INF/schema.xsd"/>
        </bean>
    
    </beans>
    

    web.xml

    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
             version="2.4">
    
        <display-name>MyCompany HR Holiday Service</display-name>
    
        <servlet>
            <servlet-name>spring-ws</servlet-name>
            <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>spring-ws</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    
    </web-app>
    

    To get the response returned as a SAAJ attachment rather than an MTOM attachment, i have to manually configure the SAAj factories as described in this thread How do I add an attachment to a response payload in Spring-WS?

    0 讨论(0)
提交回复
热议问题