2013-02-28 189 views
7

我是Apache CXF和tomcat的新手。我正在嘗試構建一個簡單的Web服務並將其部署到Tomcat上。下面是我的web.xml 但是,當我嘗試使用我的瀏覽器訪問'services'文件夾時,它說沒有找到服務。我嘗試創建Java Web服務客戶端,但它無法找到服務。這可能是什麼錯誤?Apache CXF和tomcat

<?xml version="1.0" encoding="UTF-8"?> 
<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_2_5.xsd"> 
    <display-name>Sample web service provider</display-name> 
    <listener> 
     <!-- For Metro, use this listener-class instead: 
      com.sun.xml.ws.transport.http.servlet.WSServletContextListener --> 
     <listener-class> 
       org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener> 
    <!-- Remove below context-param element if using Metro --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
       classpath:META-INF/cxf/cxf.xml 
     </param-value> 
    </context-param> 
    <servlet> 
     <servlet-name>WebServicePort</servlet-name> 
     <!-- For Metro, use this servlet-class instead: 
      com.sun.xml.ws.transport.http.servlet.WSServlet --> 
     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>WebServicePort</servlet-name> 
     <url-pattern>/services/*</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout>60</session-timeout> 
    </session-config> 
</web-app> 

回答

6

這意味着您的應用程序中沒有公開任何服務。你的web.xml似乎是正確的,但我錯過了一件事,你的Spring配置。添加您的Spring配置位置在web.xml,用於例如: -

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

此外,你必須創建一個類,將實現你的Web服務接口,並將其作爲在Spring applicationContext.xml配置文件CXF端點。對於例如爲:

<bean id="candidateImpl" class="some.pckg.CandidateImpl"/> 

<jaxws:endpoint id="candidateEndpoint" 
       implementor="#candidateImpl" 
       address="/Candidate" 
     /> 

CandidateImpl類應該有@WebService註解。對於如:

@WebService(targetNamespace = "http://something.com/ws/candidate", 
     portName = "CandidateService", 
     serviceName = "Candidate", 
     endpointInterface = "some.pckg.types.CandidateService", 
     wsdlLocation = "WEB-INF/wsdl/CandidateService.wsdl") 
public class CandidateImpl implements CandidateService { 
    //Implementation of all methods from CandidateService. 
} 

如果你正確地做了一切,你應該看到有下提供的一項服務:

http(s)://whateverhost.com:<somePort>/SomeContextPath/services 

,你應該能夠得到WSDL文件是這樣的:

http(s)://whateverhost.com:<somePort>/SomeContextPath/services/Candidate?wsdl 

參見:

1

您需要設置Spring配置文件的位置,使這項工作。您可以如下設置它。

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

您需要在web.xml中配置一個servlet。下面是一個例子。

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE web-app 
     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
     "http://java.sun.com/dtd/web-app_2_3.dtd"> 
<web-app> 

    <servlet> 
     <servlet-name>CXFServlet</servlet-name> 
     <display-name>CXF Servlet</display-name> 
     <servlet-class> 
      org.apache.cxf.transport.servlet.CXFServlet 
     </servlet-class> 
     <init-param> 
      <param-name>config-location</param-name> 
      <param-value>/WEB-INF/spring-ws-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>CXFServlet</servlet-name> 
     <url-pattern>/services/*</url-pattern> 
    </servlet-mapping> 

</web-app> 

現在您需要在WEB-INF文件夾下定義一個名爲spring-ws-servlet.xml的文件。下面是一個spring-ws-servlet.xml的內容示例,其中包含Web服務的實際配置。這取決於你的邏輯,當然是:

<?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:context="http://www.springframework.org/schema/context" 
     xmlns:jaxws="http://cxf.apache.org/jaxws" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://cxf.apache.org/jaxws 
     http://cxf.apache.org/schemas/jaxws.xsd"> 

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

    <!-- JAX-WS Service Endpoint --> 
    <bean id="personImpl" class="com.sample.service.impl.PersonServiceImpl"/> 

    <jaxws:endpoint id="personEndpoint" 
        implementor="#personImpl" 
        address="/person"> 
     <jaxws:properties> 
      <entry key="schema-validation-enabled" value="true"/> 
     </jaxws:properties> 
    </jaxws:endpoint> 
    <!-- JAX-WS Service Endpoint End--> 
</beans> 

有了這個,你可以在http://localhost:8080/services/person?wsdl

這是從這個職位採取了訪問Web服務。這是一個關於與IntelliJ IDEA的和Spring

https://aldavblog.wordpress.com/2015/01/22/creating-a-web-service-from-scratch-using-spring-maven-apache-cxf/

+0

雖然此鏈接可以回答這個問題,最好是在這裏有答案的主要部件,並提供鏈接以供參考創建CXF服務教程。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – tbodt 2015-01-22 19:25:12

+0

好的,我已經更新了缺少的信息的答案。希望能幫助到你。 – 2015-01-22 19:33:13