带有 MTOM 附件的 Spring-WS Web 服务 - Hello world 测试

2023-11-24

我正在尝试创建一个简单的 Spring Web 服务,调用该服务时会返回一个文件附件作为 SOAP 响应的一部分。 Endpoint类如下所示:

最后是终点

@PayloadRoot(namespace="http://ws.mypackage.com", localPart="downloadMessageRequest")
    @ResponsePayload
    public JAXBElement<DownloadResponseType> invoke(@RequestPayload DownloadMessageRequest req) throws Exception  {

        DownloadResponseType response = new DownloadResponseType();
        DownloadResponseType.PayLoad payload = new DownloadResponseType.PayLoad();          

        javax.activation.DataHandler dataHandler = new javax.activation.DataHandler(new FileDataSource("c:\\temp\\maven-feather.png"));
        payload.setMessagePayLoad(dataHandler);
        response.setPayLoad(payload);

        return objectFactory.createDownloadMessageResponse(response);

    }

我希望响应将文件作为附件包含在内,类似于以下响应:

Content-Type: multipart/related; boundary=MIMEBoundary4A7AE55984E7438034;
                         type="application/xop+xml"; start="<[email protected]>";
                         start-info="text/xml; charset=utf-8"

--MIMEBoundary4A7AE55984E7438034
content-type: application/xop+xml; charset=utf-8; type="application/soap+xml;"
content-transfer-encoding: binary
content-id: <[email protected]>

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="...."....>
  ........
         <xop:Include href="cid:[email protected]" 
                        xmlns:xop="http://www.w3.org/2004/08/xop/include">
         </xop:Include>
  ........

</soapenv:Envelope>
--MIMEBoundary4A7AE55984E7438034
content-type: application/octet-stream
content-transfer-encoding: binary
content-id: <[email protected]>

Binary Data.....
--MIMEBoundary4A7AE55984E7438034--

我尝试遵循 spring-ws 示例中的文档和示例代码,由于某种原因,我得到的输出始终是这样的(即,base64 数据不是附件。

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: text/xml;charset=utf-8
Content-Length: 4750
Date: Tue, 03 Jul 2012 17:05:21 GMT

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:downloadMessageResponse xmlns:ns2="http://ws.mypackage.com"><ns2:payLoad><ns2:messagePayLoad>....iVBORw0KGgoAAAANSUhEUgAAAFoAAAAeCyAAAAAElFTkSuQmCC....</ns2:messagePayLoad></ns2:payLoad></ns2:downloadMessageResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

如您所见,有效负载不是附件。这是我配置应用程序的方式:

web.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/app-config.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>webservice</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/ws-config.xml</param-value>
        </init-param>
    </servlet>

ws-config.xml

<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:annotation-driven/>


    <ws:dynamic-wsdl id="serviceDefinition" portTypeName="myService"
                     locationUri="http://localhost:8080/springWsTest/webservice">
        <ws:xsd location="/WEB-INF/schemas/downloadMessageRequest.xsd"/>
    </ws:dynamic-wsdl>

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="com.mypackage.ws"/>
        <property name="mtomEnabled" value="true"/>
    </bean> 
</beans>

下载MessageRequest.xsd 架构文件

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:m="http://ws.mypackage.com" 
xmlns:xmime="http://www.w3.org/2005/05/xmlmime" elementFormDefault="qualified"
targetNamespace="http://ws.mypackage.com" 
attributeFormDefault="unqualified"> 

    <xs:element name="downloadMessageRequest">
        <xs:complexType/>
    </xs:element>

    <xs:element name="downloadMessageResponse" type="m:downloadResponseType" />

    <xs:complexType name="downloadResponseType">
            <xs:sequence>
                <xs:element name="requestName" type="xs:string"/>
                <xs:element name="payLoad">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="messagePayLoad" type="xs:base64Binary" xmime:expectedContentTypes="application/octet-stream"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
     </xs:complexType>


     <xs:element name="localDTMRequest">
        <xs:complexType/>
    </xs:element>

    <xs:element name="localDTMResponse">
        <xs:complexType>        
            <xs:sequence>
                <xs:element name="localDTM" type="xs:dateTime"/>
            </xs:sequence>          
        </xs:complexType>
    </xs:element>


</xs:schema>

该文件确实被转换为 base64binary。 JAXB 类已正确生成。端点可以工作,但它不包含该文件作为附件。即使我设置了 mtomEnabled=true,它仍将其作为 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" 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>

我添加的更改基于我在本文中读到的内容 - blog.hpxn.net/2012/06/

编辑 - 一个例子

下面是一个以 MTOM 格式返回附件的示例(基于 spring 示例)。我刚刚尝试了一下,响应如下所示:

   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: <[email protected]>
Content-Transfer-Encoding: binary

‰PNG

请注意,我尚未配置任何 Axiom 工厂。下面列出了必要的文件:

架构.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>

为了获得作为 SAAJ 附件而不是 MTOM 附件返回的响应,我必须按照此线程中的描述手动配置 SAAj 工厂如何在 Spring-WS 中向响应负载添加附件?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有 MTOM 附件的 Spring-WS Web 服务 - Hello world 测试 的相关文章

随机推荐