使用Spring整合CXF,开发rest的服务
一、服务端开发
1、创建Dynamic Web Project,具体略
2、创建pojo类
package com.xxx.cxf.pojo;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "student")
public class Student {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3、创建服务接口
package com.xxx.cxf.service;
import javax.jws.WebService;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.xxx.cxf.pojo.Student;
@WebService
@Path("/student")
public interface StudentInterface {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/query/{id}")
public Student query(@PathParam("id")long id);
}
4、创建实现类
package com.xxx.cxf.service;
import com.xxx.cxf.pojo.Student;
public class StudentInterfaceImpl implements StudentInterface {
@Override
public Student query(long id) {
Student student = new Student();
student.setId(2);
student.setName("hehe");
return student;
}
}
5、在Spring的配置文件中,发布服务
<?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:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- <jaxrs:server发布REST的服务 ,对JAXRSServerFactoryBean类封装-->
<jaxrs:server address="/user">
<jaxrs:serviceBeans>
<ref bean="studentInterface"/>
</jaxrs:serviceBeans>
</jaxrs:server>
<!-- 配置服务实现类 -->
<bean name="studentInterface" class="com.xxx.cxf.service.StudentInterfaceImpl"/>
</beans>
6、配置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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ws_2_cxf_spring_server</display-name>
<!-- 设置spring的环境 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置CXF的Servlet -->
<servlet>
<servlet-name>CXF</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXF</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
7、部署到Tomcat,查看使用说明书
http://localhost:8080/zzzzz_cxf_spring_rest_server/ws/user?_wadl
二、客户端开发