09.CXF发布WebService(Web项目)

在web项目中创建类的cxf服务

1.创建web项目
2.导入所有包
3.创建服务类,必须指定注解@webService
4.配置web.xml

  1. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
  2. <web-app>
  3. <servlet>
  4. <servlet-name>cxf</servlet-name>
  5. <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  6. </servlet>
  7. <servlet-mapping>
  8. <servlet-name>cxf</servlet-name>
  9. <url-pattern>/services/*</url-pattern>
  10. </servlet-mapping>
  11. <session-config>
  12. <session-timeout>60</session-timeout>
  13. </session-config>
  14. </web-app>

5.创建cxf的核心配置文件cxf-servlet.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
  4. xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
  8. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
  9. http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
  10. <!-- 引入CXF Bean定义如下,早期的版本中使用 -->
  11. <import resource="classpath:META-INF/cxf/cxf.xml" />
  12. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  13. <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  14. <jaxws:endpoint id="hello" address="/hello" implementor="com.rl.cxf.web.server.HelloService">
  15. <jaxws:outInterceptors>
  16. <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
  17. </jaxws:outInterceptors>
  18. <jaxws:inInterceptors>
  19. <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
  20. </jaxws:inInterceptors>
  21. </jaxws:endpoint>
  22. </beans>

在web项目中创建接口的cxf服务

1.创建服务接口在接口上加@webservice
2.创建服务接口的实现类
3.在web.xml中配置CXFServlet
4.配置cxf-servlet.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
  4. xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
  8. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
  9. http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
  10. <!-- 引入CXF Bean定义如下,早期的版本中使用 -->
  11. <import resource="classpath:META-INF/cxf/cxf.xml" />
  12. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  13. <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  14. <jaxws:server id="bye" address="/bye" serviceClass="com.rl.cxf.web.inter.ByeInter">
  15. <jaxws:serviceBean>
  16. <bean class="com.rl.cxf.web.inter.ByeInterImpl"></bean>
  17. </jaxws:serviceBean>
  18. <jaxws:outInterceptors>
  19. <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
  20. </jaxws:outInterceptors>
  21. <jaxws:inInterceptors>
  22. <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
  23. </jaxws:inInterceptors>
  24. </jaxws:server>
  25. </beans>

cxf配置文件位置和名称的修改

1.第一种方式

  1. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
  2. <web-app>
  3. <servlet>
  4. <servlet-name>cxf</servlet-name>
  5. <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  6. <init-param>
  7. <param-name>config-location</param-name>
  8. <param-value>classpath:cxf.xml</param-value>
  9. </init-param>
  10. <load-on-startup>1</load-on-startup>
  11. </servlet>
  12. <servlet-mapping>
  13. <servlet-name>cxf</servlet-name>
  14. <url-pattern>/services/*</url-pattern>
  15. </servlet-mapping>
  16. </web-app>

2.第二种方式

  1. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
  2. <web-app>
  3. <listener>
  4. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  5. </listener>
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:cxf.xml</param-value>
  9. </context-param>
  10. <servlet>
  11. <servlet-name>cxf</servlet-name>
  12. <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  13. </servlet>
  14. <servlet-mapping>
  15. <servlet-name>cxf</servlet-name>
  16. <url-pattern>/services/*</url-pattern>
  17. </servlet-mapping>
  18. </web-app>
posted @ 2017-02-24 12:53  Wesly186  阅读(139)  评论(0编辑  收藏  举报