복붙노트

[SPRING] MTOM 첨부 파일이있는 Spring-WS 웹 서비스 - Hello world test

SPRING

MTOM 첨부 파일이있는 Spring-WS 웹 서비스 - Hello world test

호출 될 때 SOAP 응답의 일부로 첨부 파일을 반환하는 간단한 Spring 웹 서비스를 만들려고합니다. 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="<0.09BC7F4BE2E4D3EF1B@apache.org>";
                         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: <0.09BC7F4BE2E4D3EF1B@apache.org>

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="...."....>
  ........
         <xop:Include href="cid:1.A91D6D2E3D7AC4D580@apache.org" 
                        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: <1.A91D6D2E3D7AC4D580@apache.org>

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

스프링 - 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>

보시다시피, 페이로드는 첨부 파일이 아닙니다. 다음은 내 애플리케이션을 구성한 방법입니다.

을 포함한다.

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

downloadMessageRequest.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 태그의 일부로 포함됩니다.

내가 뭘 놓치고 있니?

해결법

  1. ==============================

    1.마침내 그걸 작동시킬 수있었습니다. 구성은 원래 게시물에있는 것과 거의 비슷합니다. 구성 파일을 업데이트해야했습니다. 여기 내 설정 파일이 어떻게 생겼는지 보여줍니다.

    마침내 그걸 작동시킬 수있었습니다. 구성은 원래 게시물에있는 것과 거의 비슷합니다. 구성 파일을 업데이트해야했습니다. 여기 내 설정 파일이 어떻게 생겼는지 보여줍니다.

    <?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 형식으로 반환하는 예제 (스프링 샘플을 기반으로 함)입니다. 방금 시도했는데 응답은 아래와 같습니다.

       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
    

    Axiom 팩토리는 구성하지 않았습니다. 필요한 파일은 다음과 같습니다.

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

    응답을 MTA 첨부 파일이 아닌 SAAJ 첨부 파일로 반환하려면이 스레드에서 설명한대로 수동으로 SAAj 팩토리를 구성해야합니다. Spring-WS에서 응답 페이로드에 첨부 파일을 추가하려면 어떻게해야합니까?

  2. from https://stackoverflow.com/questions/11316023/spring-ws-webservice-with-mtom-attachment-hello-world-test by cc-by-sa and MIT license