CXF发布RestFul WebService和SOAP WebService
#
CXF发布RestFul WebService和SOAP WebService
Apache CXF可以发布多种协议的WebService,Spring支持整合cxf到项目中,可以简化后台构架,以下是一个cxf发布SOAP协议WebService和RestFul 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-frontend-jaxrs</artifactId>
<version>3.1.11</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1-m07</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.0.pr3</version>
</dependency>
二、web.xml中添加CXFServlet拦截处理请求
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
三、开发RestFul风格的WebService
1) RestFul服务接口定义
@Consumes("application/json")
@Produces("application/json")
public interface IStudentService {
/**
*
* @param student
* @param page
* @return
*/
@GET
@Path(value = "/findStudentList/{pageSize}/{curPage}")
public List<StudentVO> findStudent(@QueryParam("") StudentVO student, @PathParam("") PageVO page);
/**
*
* @param students
*/
@POST
@Path(value = "/addStudent")
@Consumes("application/json")
@Produces("application/json")
public void addStudent(List<StudentVO> students);
}
2) RestFul服务接口实现
@Path("/service")
@Component
public class StudentService implements IStudentService {
@Override
public List<StudentVO> findStudent(StudentVO student, PageVO page) {
List<StudentVO> list = new ArrayList<StudentVO>();
StudentVO s1 = new StudentVO();
s1.setId(1L);
s1.setName("name1");
s1.setNo("090440112");
s1.setGrade(1);
list.add(s1);
return list;
}
@Override
public void addStudent(List<StudentVO> students) {
}
}
3) 发布RestFul服务
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
">
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
<jaxrs:server id="studentRestService" address="/student">
<jaxrs:serviceBeans>
<ref bean="studentService"></ref>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider"/>
</jaxrs:providers>
</jaxrs:server>
</beans>
4) 调用RestFul服务
四、开发SOAP协议的WebService
1) SOAP服务接口定义
@WebService
public interface ICourseService {
@Path("/findCourseList")
public String findCourseList(String req);
}
2) SOAP服务接口实现
@Component
public class CourseService implements ICourseService {
@Override
public String findCourseList(String req) {
return "course list";
}
}
3) 发布SOPA服务
Spring配置发布如下,也可以使用jaxws:endpoint的方式配置发布服务
<jaxws:server id="courseWsService" serviceClass="com.ttt.server.logistics.security.service.ICourseService" address="/course">
<jaxws:serviceBean>
<ref bean="courseService"/>
</jaxws:serviceBean>
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:server>
4) 调用SOAP服务
Spring中配置Client,通过注入Client到其他Bean中,调用SOAP服务
<jaxws:client id="courseWebService" serviceClass="com.ttt.server.logistics.security.service.ICourseService" address="http://127.0.0.1:8080/ttt.server/services/course">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:client>
使用JaxWsProxyFactoryBean调用SOAP服务
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(ICourseService.class);
factory.setAddress("http://127.0.0.1:8080/ttt.server/services/course");
ICourseService server = (ICourseService) factory.create();
System.out.println(server.findCourseList("111"));
完。
还没有评论,来说两句吧...