如何在Spring-ws

时间:2016-04-20 15:21:32

标签: java spring soap wsdl spring-ws

我正在使用Spring 4将系统迁移到Java,我需要在WSDL上创建一些Soap Services。

我的问题是我的客户端产生的WSDL 在模式中没有任何请求或响应(complextype)标记而Spring-ws默认情况下不会产生任何消息(服务) wsdl使用原始模式,因为它在模式的值中查找Resquest和Response后缀。

现在我的客户端的WSDL看起来像这样:

    <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="http://mypackage.com/myService"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:tns="http://mypackage.com/myService"
                  targetNamespace="http://mypackage.com/myService">
    <wsdl:types>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                elementFormDefault="qualified"
                targetNamespace="http://mypackage.com/myService"
                xmlns:hr="http://mypackage.com/myService">
            <element name="element1" type="string"/>
            <element name="element2" type="string"/>
            <element name="elementReturn1" type="string"/>
        </schema>
    </wsdl:types>

    <wsdl:message name="service1Request">
        <wsdl:part element="tns:element1" name="element1"/>
        <wsdl:part element="tns:element2" name="element2"/>
    </wsdl:message>
    <wsdl:message name="service1Response">
        <wsdl:part element="tns:elementReturn1" name="elementReturn1"/>
    </wsdl:message>

    <wsdl:portType name="myService">
        <wsdl:operation name="service1">
            <wsdl:input message="tns:service1Request" name="service1Request"/>
            <wsdl:output message="tns:service1Response" name="service1Response"/>
        </wsdl:operation>
    </wsdl:portType>

    <wsdl:binding name="myServiceSoap11" type="tns:myService">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="service1">
            <soap:operation soapAction=""/>
            <wsdl:input name="service1Request">
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output name="service1Response">
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="myService">
        <wsdl:port binding="tns:myServiceSoap11" name="myServiceSoap11">
            <soap:address location="http://localhost:7001/mydomain/"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

默认情况下使用此模式生成的WSDL spring-ws如下所示:

    <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="http://mypackage.com/myService"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:tns="http://mypackage.com/myService"
                  targetNamespace="http://mypackage.com/myService">
    <wsdl:types>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                elementFormDefault="qualified"
                targetNamespace="http://mypackage.com/myService"
                xmlns:hr="http://mypackage.com/myService">
            <element name="element1" type="string"/>
            <element name="element2" type="string"/>
            <element name="elementReturn1" type="string"/>
        </schema>
    </wsdl:types>

    <wsdl:portType name="myService"></wsdl:portType>

    <wsdl:binding name="myServiceSoap11" type="tns:myService">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    </wsdl:binding>

    <wsdl:service name="myService">
        <wsdl:port binding="tns:myServiceSoap11" name="myServiceSoap11">
            <soap:address location="http://localhost:7001/mydomain/"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

正如您所注意到的,生成的wsdl spring-ws不会产生任何消息,这意味着没有消费服务。

你能否告诉我如何告诉Spring-ws制作肥皂信息(请求和响应)而不在模式中指定所以我可以构建相同的wsdl结构?

为了构建相同的Web服务,我手动将Request和Response标记添加到我的模式中,我生成的wsdl如下所示:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="http://mypackage.com/myService" 
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
                  xmlns:tns="http://mypackage.com/myService" 
                  targetNamespace="http://mypackage.com/myService">
    <wsdl:types>
        <schema xmlns="http://www.w3.org/2001/XMLSchema"
                elementFormDefault="qualified"
                targetNamespace="http://mypackage.com/myService"
                xmlns:hr="http://mypackage.com/myService">
            <element name="service1Request">
                <complexType>
                    <sequence>
                        <element ref="hr:element1"/>
                        <element ref="hr:element2"/>
                    </sequence>
                </complexType>
            </element>
            <element name="service1Response">
                <complexType>
                    <sequence>
                        <element ref="hr:elementReturn1"/>
                    </sequence>
                </complexType>
            </element>

            <element name="element1" type="string"/>
            <element name="element2" type="string"/>
            <element name="elementReturn1" type="string"/>
        </schema>
    </wsdl:types>

    <wsdl:message name="service1Request">
        <wsdl:part element="tns:service1Request" name="service1Request"/>
    </wsdl:message>
    <wsdl:message name="service1Response">
        <wsdl:part element="tns:service1Response" name="service1Response"/>
    </wsdl:message>

    <wsdl:portType name="myService">
        <wsdl:operation name="service1">
            <wsdl:input message="tns:service1Request" name="service1Request"/>
            <wsdl:output message="tns:service1Response" name="service1Response"/>
        </wsdl:operation>
    </wsdl:portType>

    <wsdl:binding name="myServiceSoap11" type="tns:myService">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="service1">
            <soap:operation soapAction=""/>
            <wsdl:input name="service1Request">
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output name="service1Response">
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="myService">
        <wsdl:port binding="tns:myServiceSoap11" name="myServiceSoap11">
            <soap:address location="http://localhost:7001/mydomain/"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

在使用其中一项服务方面,第一项(我的客户)的输入将是这样的

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:act="http://mypackage.com/myService">
   <soapenv:Header/>
   <soapenv:Body>
       <act:element1>A</act:element1>
       <act:element2>B</act:element2>
   </soapenv:Body>
</soapenv:Envelope>

虽然spring-ws看起来像这样

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:act="http://mypackage.com/myService">
   <soapenv:Header/>
   <soapenv:Body>
      <act:service1Request>
         <act:element1>A</act:element1>
         <act:element2>B</act:element2>
      </act:service1Request>
   </soapenv:Body>
</soapenv:Envelope>

我的 web.xml 是:

<web-app  version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app.xsd">

    <display-name>Spring3 MVC Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:MyContext.xml</param-value>
    </context-param>

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

    <servlet>
        <servlet-name>spring-ws</servlet-name>
        <servlet-class>
            org.springframework.ws.transport.http.MessageDispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>transformWsdlLocations</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-ws</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

我还有一个 spring-ws-servlet.xml ,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:sws="http://www.springframework.org/schema/web-services"
        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.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/web-services
                            http://www.springframework.org/schema/web-services/web-services.xsd">

    <sws:annotation-driven />

    <context:component-scan base-package="com.mypackage"/>

    <bean id="myService" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
        <property name="schema" ref="mySchema"/>
        <property name="portTypeName" value="myService"/>
        <property name="locationUri" value="/"/>
        <property name="targetNamespace" value="http://mypackage.com/myService"/>
    </bean>

    <bean id="mySchema" class="org.springframework.xml.xsd.SimpleXsdSchema">
        <property name="xsd" value="classpath:schemas/mySchema.xsd"/>
    </bean>
</beans>

PD:我正在使用Weblogic,这就是为什么我在Spring上使用XML配置而不是Java代码的原因。

提前致谢!

修改

我做了你的建议,我可以按照我的期望部署我的wsdl,但现在我可以匹配我的端点

我的项目有下一个文件:

的web.xml

<web-app  version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app.xsd">

    <display-name>Spring4 Soap</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:SpringConfig.xml
        </param-value>
    </context-param>

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

    <servlet>
        <servlet-name>spring-ws</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
        <init-param>
            <param-name>transformWsdlLocations</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-ws</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

弹簧-WS-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:sws="http://www.springframework.org/schema/web-services"
       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.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/web-services
                            http://www.springframework.org/schema/web-services/web-services.xsd">

    <sws:annotation-driven />

    <context:component-scan base-package="com.mypackage.myService"/>

    <bean id="myService" class="org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition">
        <property name="wsdl" value="classpath:example.wsdl" />
    </bean>

</beans>

ServiceEnpoint

@Endpoint
public class ServiceEndpoint {

    private static final String NAMESPACE_URI = "http://mypackage.com/myService";

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "service1Request")
    @ResponsePayload
    public String getEstadoDocRequest(String element1,
                                      String element2) {
        return  element1 + " " + element2;
    }
}

现在当我尝试通过SoapUI测试我的服务时,我收到了下一个错误:

ADVERTENCIA:找不到[SaajSoapMessage {http://mypackage.com/myService} element1]的端点映射 &lt; 27-04-2016 10:50:28 Hora de Chile&gt; http://mypackage.com/myService} element1]&gt;

正如您所知,Spring正在寻找请求,并且它不知道element1是request1的元素。

如何告诉Spring哪个是请求,以便它可以将我的请求与我的端点匹配

谢谢!

0 个答案:

没有答案