简体   繁体   English

Apache CXF和tomcat

[英]Apache CXF and tomcat

I am fairly new to Apache CXF and tomcat. 我是Apache CXF和tomcat的新手。 I am trying to build a simple web service and deploy it on tomcat. 我正在尝试构建一个简单的Web服务并将其部署在tomcat上。 below is my web.xml However when I try to access the 'services' folder using my browser it says No services have been found. 下面是我的web.xml但是当我尝试使用我的浏览器访问'services'文件夹时,它说没有找到服务。 I tried creating java web service client but it is not able to locate the service either. 我尝试创建java Web服务客户端,但它也无法找到服务。 What could be wrong in this? 这可能有什么问题?

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

This means that you don't have any services exposed in your application. 这意味着您的应用程序中没有公开任何服务。 Your web.xml seems to be correct but I've just missed one thing, your Spring configuration. 你的web.xml似乎是正确的,但我只是错过了一件事,你的Spring配置。 Add your Spring config location in your web.xml , for eg: 在您的web.xml添加Spring配置位置,例如:

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

Also, you have to create a class which will implement your web service interface and expose it as the CXF endpoint in your Spring applicationContext.xml configuration file. 此外,您必须创建一个类,该类将实现您的Web服务接口,并将其作为Spring applicationContext.xml配置文件中的CXF端点公开。 For eg: 例如:

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

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

Your CandidateImpl class should have @WebService annotation. 您的CandidateImpl类应该有@WebService注释。 For eg: 例如:

@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.
}

If you've done everything correctly you should see that there is one service available under: 如果您已正确完成所有操作,您应该会看到有以下服务可用:

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

And you should be able to get the WSDL file like this: 你应该能够得到这样的WSDL文件:

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

See also: 也可以看看:

You need to set the spring configuration file location to make this work. 您需要设置弹簧配置文件位置才能使其工作。 You can set it as follows. 您可以按如下方式进行设置。

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

You need to configure a servlet in your web.xml. 您需要在web.xml中配置servlet。 Below an example. 下面是一个例子。

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

Now you need to define a file named spring-ws-servlet.xml under WEB-INF folder. 现在,您需要在WEB-INF文件夹下定义名为spring-ws-servlet.xml的文件。 Below an example of the content of spring-ws-servlet.xml, which contains the actual configuration for your web service. 下面是spring-ws-servlet.xml内容的示例,其中包含Web服务的实际配置。 This depends on your logic, of course: 当然,这取决于你的逻辑:

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

With this, you can access your web service under http://localhost:8080/services/person?wsdl 有了这个,您可以在http:// localhost:8080 / services / person?wsdl下访问您的Web服务

This is taken from this post. 这是从这篇文章中获取的。 It is a tutorial about creating a Cxf service with IntelliJ Idea and Spring 这是一个关于使用IntelliJ Idea和Spring创建Cxf服务的教程

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM