【问题标题】:Apache CXF and tomcatApache CXF 和 tomcat
【发布时间】:2013-02-28 03:25:46
【问题描述】:

我对 Apache CXF 和 tomcat 还很陌生。我正在尝试构建一个简单的 Web 服务并将其部署在 tomcat 上。下面是我的 web.xml 但是,当我尝试使用浏览器访问“服务”文件夹时,它显示未找到任何服务。我尝试创建 java web 服务客户端,但它也无法找到该服务。这有什么问题?

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

【问题讨论】:

    标签: java tomcat cxf


    【解决方案1】:

    这意味着您的应用程序中没有公开任何服务。您的 web.xml 似乎是正确的,但我只是错过了一件事,您的 Spring 配置。在 web.xml 中添加 Spring 配置位置,例如:

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

    另外,您必须创建一个类来实现您的 Web 服务接口,并将其作为 CXF 端点在 Spring applicationContext.xml 配置文件中公开。例如:

    <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
    

    另请参阅:

    【讨论】:

      【解决方案2】:

      您需要设置 spring 配置文件位置才能使其工作。可以如下设置。

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

      【讨论】:

        【解决方案3】:

        您需要在 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下访问你的网络服务

        这取自这篇文章。这是一个关于使用 IntelliJ Idea 和 Spring 创建 Cxf 服务的教程

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

        【讨论】:

        • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
        • 好的,我已经用缺失的信息更新了答案。希望对您有所帮助。
        猜你喜欢
        • 2021-06-15
        • 1970-01-01
        • 1970-01-01
        • 2014-09-22
        • 2012-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-27
        相关资源
        最近更新 更多