前面文章记录了在spring中如何整合cxf开发webservice客户端和服务端,其实,相较于原生开发方式,已经有了不少优化。rest风格的开发,作为一种极为流行的开发规范,可以帮助我们更加简洁高效的发布服务,接收服务。
客户端
- 添加依赖
之前的都是jaxws,现在都将w编程了r,也就是rest的意思。
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.3.5</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-rs-client -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-rs-extension-providers -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.jettison/jettison -->
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.4.0</version>
</dependency>
- 配置web.xml
和之前的没有更多的变动
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>cXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cXFServlet</servlet-name>
<url-pattern>/webService/*</url-pattern>
</servlet-mapping>
<!-- 2.配置spring容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
<!-- 3.监听器-->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- 编写实体类
@XmlRootElement注解指定了对象序列化为json/xml过后的根节点。
@XmlRootElement(name="student")
public class Student {
private Integer id;
private String name;
private String gender;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
}
- service接口和实现类
IStudnetService 接口,其中: @Path表示访问当前服务接口对应的路径; @Consumes指定服务器支持的接收的数据类型; @Produces指定服务器支持的返回的数据类型
public interface IStudnetService {
/**
* post对应的是insert操作
* get对应的是select操作
* put对应的是update操作
* delete对应的是delete操作
*
* @param student
*/
@POST
@Path("/student")
@Consumes({"application/xml","application/json"})
public void addStudent(Student student);
@PUT
@Path("/student")
@Consumes({"application/xml","application/json"})
public void saveStudent(Student student);
@GET
@Path("/student/{id}")
@Consumes({"application/xml","application/json"})
@Produces({"application/xml","application/json"})
public Student getStudentById(@PathParam("id") Integer id);
@GET
@Path("/student")
@Produces({"application/xml","application/json"})
public List<Student> getStudent();
@DELETE
@Path("/student/{id}")
@Consumes({"application/xml","application/json"})
public void deleteStudent(@PathParam("id") Integer id);
}
StudentService 实现类
这里模拟rest风格的增删改查,当客户端调用服务时,服务端打印日志记录。
public class StudentService implements IStudnetService {
@Override
public void addStudent(Student student) {
System.out.println(student.getName()+"学生信息添加成功!");
}
@Override
public void saveStudent(Student student) {
System.out.println(student.getName()+"学生信息修改成功!");
}
@Override
public Student getStudentById(Integer id) {
Student student = new Student();
student.setId(id);
student.setAge(13);
student.setGender("男");
student.setName("william");
System.out.println("id为"+id+"学生信息查询成功!");
return student;
}
@Override
public List<Student> getStudent() {
Student student1 = new Student();
student1.setId(1);
student1.setAge(13);
student1.setGender("男");
student1.setName("william");
Student student2 = new Student();
student2.setId(2);
student2.setAge(12);
student2.setGender("女");
student2.setName("elaine");
List<Student> studentList = new ArrayList<Student>();
studentList.add(student1);
studentList.add(student2);
System.out.println("成功查询所有学生信息成功!");
return studentList;
}
@Override
public void deleteStudent(Integer id) {
System.out.println("id为"+id+"的学生信息删除成功!");
}
}
- 配置applicationContext.xml
配置服务的方式基本没有什么变化,需要注意一下jaxrs标签以及对应的命名空间。
<?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:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd ">
<jaxrs:server address="/studentService">
<jaxrs:serviceBeans>
<bean class="com.wuwl.service.impl.StudentService"></bean>
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
- 启动tomcat服务
启动成功后,我们通过浏览器访问:http://localhost:8081/webService/studentService/student/123
,会在页面上显示:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<student>
<age>13</age>
<gender>男</gender>
<id>123</id>
<name>william</name>
</student>
客户端
客户端的编写则更加简洁明了了。
7. 引入相关的依赖
8. 导入实体类
这里需要导入Student
这个实体类。
9. 编写测试代码Client
通过webClient对象调用服务,发送不同的请求类型,自动匹配对应的服务方法
public class Client {
@Test
public void testPost(){
Student st1 = new Student();
st1.setId(10);
st1.setName("jack");
st1.setGender("男");
st1.setAge(15);
//通过webClient对象远程调用服务
WebClient.create("http://localhost:8081/webService/studentService/student").type(MediaType.APPLICATION_JSON).post(st1);
}
@Test
public void testDelete(){
WebClient.create("http://localhost:8081/webService/studentService/student/12").type(MediaType.APPLICATION_JSON).delete();
}
@Test
public void testPut(){
Student st1 = new Student();
st1.setId(10);
st1.setName("marry");
st1.setGender("/女");
st1.setAge(15);
//通过webClient对象远程调用服务
WebClient.create("http://localhost:8081/webService/studentService/student").type(MediaType.APPLICATION_JSON).put(st1);
}
@Test
public void testGetById(){
Student student = WebClient.create("http://localhost:8081/webService/studentService/student/12").accept(MediaType.APPLICATION_JSON).get(Student.class);
System.out.println(student);
}
@Test
public void testGet(){
List<Student> studentList = (List<Student>) WebClient.create("http://localhost:8081/webService/studentService/student")
.accept(MediaType.APPLICATION_JSON).getCollection(Student.class);
System.out.println(studentList);
}
}
我们自上而下依次执行单元测试方法,由于除了GET请求以外,都没有返回值,客户端看不到任何输出,而服务端可以查看对应请求记录。
jack学生信息添加成功!
id为12的学生信息删除成功!
marry学生信息修改成功!
接着,我们测试testGetById方法。
客户端输出:
Student{id=12, name='william', gender='男', age=13}
服务端输出:
id为12学生信息查询成功!
最后,测试testGet方法。
客户端输出:
[Student{id=1, name='william', gender='男', age=13}, Student{id=2, name='elaine', gender='女', age=12}]
服务端输出:
成功查询所有学生信息成功!