Previous article documents how to integrate cxf develop webservice client and server in the spring, in fact, compared to the native way of development, already has a lot of optimization. rest style development, as a very popular development specifications, help us to be more concise and efficient distribution service, reception service.
Client
- Add depend
before all jaxws, now the program will be w r, is the rest of meaning.
<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 configuration
and no more changes before
<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>
- Write entity class
@XmlRootElement annotation specifies an object is serialized to json root / after the 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 interface and implementation class
IStudnetService interfaces, wherein: @Path represents the path of the current access corresponding service interface; receiving the data type of the server supports @Consumes; @Produces server supports the specified data type returned
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 implementation classes
here rest simulation style CRUD, when the client calls the service, the server print the log.
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+"的学生信息删除成功!");
}
}
- Configuration applicationContext.xml
configuration service way there is little change, need to take note jaxrs labels and corresponding namespace.
<?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>
- Start tomcat service
after a successful start, we accessed through a browser:http://localhost:8081/webService/studentService/student/123
will be displayed on the page:
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<student>
<age>13</age>
<gender>男</gender>
<id>123</id>
<name>william</name>
</student>
Client
Write the client is more simple and clear.
7. The introduction of its dependencies
8. The entity class introduced
here need to import Student
the entity classes.
9. Client writing test code
call service through webClient object transmitted different request types, automatically match the corresponding service method
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);
}
}
We turn top-down execution unit testing method, since in addition to the GET request, did not return a value, the client can not see any output, but the server can view the corresponding request record.
jack学生信息添加成功!
id为12的学生信息删除成功!
marry学生信息修改成功!
Next, we tested testGetById methods.
Client output:
Student{id=12, name='william', gender='男', age=13}
Server output:
id为12学生信息查询成功!
Finally, test testGet method.
Client output:
[Student{id=1, name='william', gender='男', age=13}, Student{id=2, name='elaine', gender='女', age=12}]
Server output:
成功查询所有学生信息成功!