cxf 在做企业级webservices 服务的时候确实非常好用,个人觉得比axis1, 2都好用。 虽然spring自身也提供了webservices发布方法,这里使用cxf跟spring结合,使用起来非常方便;
整体项目结果如下:
基于maven 来配置使得项目更加简单
新建一个maven 项目,补全 src下的main和test目录
-
打开web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>cxf</display-name> <servlet> <description>m CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
- 新建一个cxf-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- START SNIPPET: beans --> <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"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <jaxws:endpoint id="DepartmentService" implementor="com.qycloud.oatos.ty.department.DepartmentServiceImpl" address="/DepartmentService"/> <jaxws:endpoint id="UserService" implementor="com.qycloud.oatos.ty.user.UserServiceImpl" address="/UserService"/> </beans>
4. 补全 pom.xml ,主要是spring 的依赖和cxf,cxf 只需要以下2个
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.0.0</version> </dependency>
- 编写代码:这里我们回头看到cxf-servlet.xml 里面可发现我们只需要制定一个id,一个是实现类,一个访问地址即可。
a) 编写一个接口
```java @WebService public interface DepartmentService {
boolean updateTheOrg(String org);
boolean updateOrgsCode(String org);
} ```
b) 编写一个实现类
```java @WebService(endpointInterface = "com.qycloud.oatos.ty.department.DepartmentService") public class DepartmentServiceImpl implements DepartmentService {
@Override public boolean updateTheOrg(String org) { // TODO Auto-generated method stub return false; }
@Override public boolean updateOrgsCode(String org) { // TODO Auto-generated method stub return false; }
} ```
7.剩下的事情就是发布了。右键run on server。结果如下
cxf3.x +spring 3.x(4.x)+ maven 发布webservice 服务,布布扣,bubuko.com