【问题标题】:wsdl 2 java: deploy a web service with apache cxfwsdl 2 java:使用 apache cxf 部署 Web 服务
【发布时间】:2014-08-11 08:38:18
【问题描述】:

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

我正在遵循的步骤是:

1) 在动态 Web 项目的 WebContent 文件夹中创建一个 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) 现在我尝试在 WAR 中部署我的 Web 服务,将其导出到 tomcat 的 webapps 文件夹中,启动 tomcat 以在 localhost:8080/WS1 中查看我的服务的 wsdl,但我收到 404 错误,但我没有不明白为什么……

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

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

wsdl2java 自动生成的方法的个人实现是否可能会改变一些东西?

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

【问题讨论】:

    标签: web-services tomcat wsdl cxf wsdl2java


    【解决方案1】:

    您使用的网址似乎错误。

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

    【讨论】:

    • 我试过了,但是当我在部署的应用程序上按开始时,tomcat 管理器说:失败 - 上下文路径 /WS1 的应用程序无法启动。是否有日志文件可以让我获得有关错误的更多信息?
    • 您可以在 localohst-{运行日期}.log 或 catlina-{运行日期}.out 中检查日志目录
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多