wsdl 2 java:使用apache cxf部署Web服务

时间:2014-08-11 08:38:19

标签: web-services tomcat wsdl cxf wsdl2java

我正在尝试使用eclipse,tomcat和cxf部署一个简单的Web服务。

我正在遵循的步骤是:

1)在WebContent文件夹中的动态Web项目中创建一个wsdl文件。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.negozio.org" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.negozio.org">

<wsdl:types>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified"  targetNamespace="http://www.negozio.org" >

    <xsd:complexType name="findTicketsRequest">
        <xsd:sequence>
            <xsd:element name="from" type="xsd:string" />
            <xsd:element name="to" type="xsd:string" />
            <xsd:element name="hour" type="tns:hourType" />
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="findTicketsResponse">
        <xsd:sequence maxOccurs="unbounded">
            <xsd:element name="ID" type="tns:IDType" />
            <xsd:element name="hour" type="tns:hourType" />
            <xsd:element name="minutes" type="tns:minutesType" />
            <xsd:element name="quantity" type="xsd:integer" />
        </xsd:sequence>
    </xsd:complexType>

    <xsd:simpleType name="hourType">
        <xsd:restriction base="xsd:integer">
            <xsd:minInclusive value="0"/>
            <xsd:maxInclusive value="23"/>
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:simpleType name="minutesType">
        <xsd:restriction base="xsd:integer">
            <xsd:minInclusive value="0"/>
            <xsd:maxInclusive value="59"/>
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:simpleType name="IDType">
        <xsd:restriction base="xsd:string">
            <xsd:pattern  value="[0-9]{4}"/>
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:element name="ricercaRequest" type="tns:findTicketsRequest"></xsd:element>
    <xsd:element name="ricercaResponse" type="xsd:boolean" />

</xsd:schema>   
</wsdl:types>

<wsdl:message name="ricercaMessage">
 <wsdl:part name="ricercaMessagePart" element="tns:ricercaRequest"/>
</wsdl:message>

<wsdl:message name="ricercaResponseMessage">
 <wsdl:part  name="ricercaResponsePart" element="tns:ricercaResponse"/>
</wsdl:message>

<wsdl:portType name="ServiceSearchPortType">
 <wsdl:operation name="findTickets">
   <wsdl:input message="tns:ricercaMessage" />
   <wsdl:output message="tns:ricercaResponseMessage" />
 </wsdl:operation>
</wsdl:portType>

<wsdl:binding name="ServiceSearchBinding" type="tns:ServiceSearchPortType">
 <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
   <wsdl:operation name="findTickets">
   <soap:operation soapAction="" />
   <wsdl:input>
     <soap:body use="literal" />
   </wsdl:input>
   <wsdl:output>
     <soap:body use="literal" />
   </wsdl:output>
 </wsdl:operation>
</wsdl:binding>

<wsdl:service name="SearchTicketsService">
  <wsdl:port  name="SearchPort" binding="tns:ServiceSearchBinding">
    <soap:address location="http://localhost:8080/WS1/ServiceSearchPortType" />
  </wsdl:port>
</wsdl:service>

</wsdl:definitions>

2)为了生成java类和方法的框架,我在名为WS1的项目文件夹中使用以下命令:  /Users/Federico/apache-cxf-2.3.3/bin/wsdl2java -wsdlLocation WebContent / WSsearch.wsdl -impl -d src / -frontend jaxws21 WebContent / WSsearch.wsdl

3)一切似乎都很好。将web.xml和beans.xml文件添加到WEB-INF文件夹:

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 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">

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

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

<servlet>
    <display-name>CXF Servlet</display-name>        
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>
        org.apache.cxf.transport.servlet.CXFServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

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

的beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<jaxws:endpoint 
  id="ricerca" 
  implementor="org.negozio.ServiceSearchPortTypeImpl" 
  address="/ServiceSearchPortType" />

</beans>

4)现在我尝试部署我的Web服务,在WAR中输入tomcat的webapps文件夹,启动tomcat,在localhost:8080 / WS1中查看我的服务的wsdl,但是我收到404错误,我不知道明白为什么......

也许我写错了wsdl,或者beans.xml不正确。

我在eclipse中有另一个具有相同构建路径的项目,它工作正常,所以我会排除库问题。我在tomcat的“lib”文件夹中复制了所有cxf库。

wsdl2java自动生成的方法的个人实现是否可能会改变某些内容?

我忘记了一些段落吗?建议或解决方案? 如果你想了解更多信息,或者我还不清楚,请问。 谢谢大家。

1 个答案:

答案 0 :(得分:4)

您使用的网址似乎有误。

  1. 登录tomcat manager http://localhost:8080/manager/html
  2. 获取Web应用程序的路径(上下文路径)。
  3. 现在浏览http://localhost:8080/[context path you got in previous step]/services
  4. servcies将列出该应用程序中所有可用的WSDL或WADL(bsically端点)。
  5. 您可以单击WSDL URL以查看WSDL