在实践中一直在使用 JAX-WS 构建 webService 服务,服务还是非常稳定、高效的。
但还是比较好奇其他的 WebService 开源框架,比如:CXF/Axis2/Spring WS 等。
源于对 Apache 的信赖和喜爱, 旗下的 CXF WebService 肯定也不会让人失望。
所以花了点时间将 CXF 引入到项目实践当中,多一种选择也未尝不可。
对于 WebService 和 CXF 简介这里就不赘述了,不太懂的同学请先移步:分分钟带你玩转 Web Services【1】JAX-WS
本篇试从 Servlet 发布 CXF WebService 和 Spring 托管 CXF WebService 两种方式,带你玩转 CXF。
Servlet 发布 CXF WebService git demo 地址:http://git.oschina.net/LanboEx/cxf-demo
Spring 托管 CXF WebService git demo 地址: http://git.oschina.net/LanboEx/cxf-spring-demo
需要有这方面实践的同学,请收藏这篇博客,到时只需将 Demo 在本地跑起来,一切就都明朗了。
a.mavn 依赖 Jar:
- <!--web 容器支持-->
- <dependency>
- <groupId>
- javax.servlet
- </groupId>
- <artifactId>
- javax.servlet-api
- </artifactId>
- <version>
- 3.1.0
- </version>
- </dependency>
- <!--apache cxf webservice-->
- <dependency>
- <groupId>
- org.apache.cxf
- </groupId>
- <artifactId>
- cxf-rt-frontend-jaxws
- </artifactId>
- <version>
- 3.1.11
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.apache.cxf
- </groupId>
- <artifactId>
- cxf-rt-transports-http
- </artifactId>
- <version>
- 3.1.11
- </version>
- </dependency>
依赖的 Jar 基本上都是 sum/apache/codehaus 支持,这些 Jar 已经过岁月洗涤,稳定高效。
b. 服务实现:
- @WebService
- @SOAPBinding(style = SOAPBinding.Style.RPC)
- public interface UserService {
- /**
- * 执行测试的WebService方法(有参)
- */
- @WebMethod
- String sayHi(@WebParam(name = "name") String name);
- }
- @WebService
- @SOAPBinding(style = SOAPBinding.Style.RPC)
- public classUserServiceImplimplements UserService {
- @WebMethod
- public String sayHi(String name) {
- return"Hi, " + name + "! ";
- }
- }
c.Servlet 实现:
- public classWebServicesServletextends CXFNonSpringServlet {
- private static final longserialVersionUID = -5314312869027558456L;
- @Override
- protected void loadBus(ServletConfig servletConfig) {
- super.loadBus(servletConfig);
- Endpoint.publish("/UserService",new UserServiceImpl());
- }
- }
你没有看错 CXF 提供的不集成 Spring 的 Servlet 就叫做 CXFNonSpringServlet,是不是有点俗。
使用起来也很简单,实现 org.apache.cxf.transport.servlet.CXFNonSpringServlet 中的 loadBus 方法即可。
d.web.xml 配置:
- <servlet>
- <servlet-name>
- cxfwsServlet
- </servlet-name>
- <servlet-class>
- com.rambo.cxf.demo.ws.servlet.WebServicesServlet
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>
- cxfwsServlet
- </servlet-name>
- <url-pattern>
- /ws/*
- </url-pattern>
- </servlet-mapping>
启动工程,访问:http://localhost:4042/cxf-demo/ws/UserService?wsdl
a.mavn 依赖 Jar:
- <!--spring-->
- <dependency>
- <groupId>
- org.springframework
- </groupId>
- <artifactId>
- spring-web
- </artifactId>
- <version>
- 4.3.7.RELEASE
- </version>
- </dependency>
- <!--apache cxf webservice-->
- <dependency>
- <groupId>
- org.apache.cxf
- </groupId>
- <artifactId>
- cxf-rt-frontend-jaxws
- </artifactId>
- <version>
- 3.1.11
- </version>
- </dependency>
- <dependency>
- <groupId>
- org.apache.cxf
- </groupId>
- <artifactId>
- cxf-rt-transports-http
- </artifactId>
- <version>
- 3.1.11
- </version>
- </dependency>
只需要依赖 SpringWeb 即可,其中 Spring Context 来托管 WebService 服务的实现类。
服务实现和 web.xml 与 Servlet 发布 是一致的,不需要进行特殊的处理,这里就不贴了。
b. 将 WebService 服务托管给 Spring Context
- <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">
- <bean id="userService" class="com.rambo.cxf.spring.demo.impl.UserServiceImpl"/>
- <jaxws:endpoint id="userServiceWs" implementor="#userService" address="/UserService"/>
- </beans>
Servlet 发布 CXF WebService 依赖的开源库较少,也就是说出问题的概率较小;
每次新增 WebService 服务需要修改 WebServicesServlet 类。
Spring Context 托管 WebService 实现类,新增服务类可配置在 cxf-servlet.xml 中;
有优秀的容器替你管理,你会很舒服,大型复杂 WebService 建议配置 Spring 使用。
来源: http://www.cnblogs.com/java-class/p/6752128.html