The previous article recorded how to integrate cxf to develop web service client and server in spring. In fact, compared with the native development method, there have been many optimizations. rest style development, as a very popular development specification, can help us more concise and efficient publishing services and receiving services.
Client
- Add dependency
Before, jaxws, now w is programmed r, which means 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>
- Configure web.xml
No more changes than 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.To configure spring container--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> <!-- 3.Monitor--> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
- Write entity class
@The XmlRootElement annotation specifies the root node after the object is serialized to 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 interface and implementation class
IStudnetService interface, where: @ Path indicates the Path corresponding to the current service interface; @ consumers specifies the received data type supported by the server; @ Produces specifies the returned data type supported by the server
public interface IStudnetService { /** * post Corresponding to the insert operation * get The corresponding operation is select * put Corresponding to the update operation * delete Corresponding to delete operation * * @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 class
Here, we simulate rest style add, delete, modify and query. When the client calls the service, the server prints the log record.
public class StudentService implements IStudnetService { @Override public void addStudent(Student student) { System.out.println(student.getName()+"Student information added successfully!"); } @Override public void saveStudent(Student student) { System.out.println(student.getName()+"Student information modified successfully!"); } @Override public Student getStudentById(Integer id) { Student student = new Student(); student.setId(id); student.setAge(13); student.setGender("male"); student.setName("william"); System.out.println("id by"+id+"Student information query succeeded!"); return student; } @Override public List<Student> getStudent() { Student student1 = new Student(); student1.setId(1); student1.setAge(13); student1.setGender("male"); student1.setName("william"); Student student2 = new Student(); student2.setId(2); student2.setAge(12); student2.setGender("female"); student2.setName("elaine"); List<Student> studentList = new ArrayList<Student>(); studentList.add(student1); studentList.add(student2); System.out.println("Successfully query all student information!"); return studentList; } @Override public void deleteStudent(Integer id) { System.out.println("id by"+id+"Successfully deleted the student information of!"); } }
- Configure applicationContext.xml
There is little change in the way services are configured. You need to pay attention to the jaxrs tag and the 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 the startup is successful, we can visit http://localhost:8081/webService/studentService/student/123 through a browser, and it will display on the page:
<?xml version="1.0" encoding="UTF-8" standalone="true"?> <student> <age>13</age> <gender>male</gender> <id>123</id> <name>william</name> </student>
Client
The writing of the client is more concise.
7. Introduce relevant dependencies
8. Import entity class
The entity class Student needs to be imported here.
9. Write test code Client
Call the service through the webClient object, send different request types, and automatically match the corresponding service methods
public class Client { @Test public void testPost(){ Student st1 = new Student(); st1.setId(10); st1.setName("jack"); st1.setGender("male"); st1.setAge(15); //Call the service remotely through the webClient object 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("/female"); st1.setAge(15); //Call the service remotely through the webClient object 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 execute unit test method from top to bottom in turn. Because there is no return value except GET request, the client can not see any output, and the server can view the corresponding request record.
jack student information added successfully! Delete the student information with id 12 successfully! marry student information modified successfully!
Next, we test the testGetById method.
Client output:
Student{id=12, name='william', gender='male', age=13}
Server output:
Student information query with id 12 succeeded!
Finally, test the testGet method.
Client output:
[Student{id=1, name='william', gender='male', age=13}, Student{id=2, name='elaine', gender='female', age=12}]
Server output:
Query all student information successfully!