Skip to content

Commit b66c42d

Browse files
committed
Apache CXF support for RESTful web services
1 parent 61ea026 commit b66c42d

File tree

9 files changed

+344
-0
lines changed

9 files changed

+344
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>cxf-jaxrs-implementation</artifactId>
7+
<parent>
8+
<groupId>com.baeldung</groupId>
9+
<artifactId>apache-cxf</artifactId>
10+
<version>0.0.1-SNAPSHOT</version>
11+
</parent>
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<cxf.version>3.1.7</cxf.version>
15+
<httpclient.version>4.5.2</httpclient.version>
16+
</properties>
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.codehaus.mojo</groupId>
21+
<artifactId>exec-maven-plugin</artifactId>
22+
<configuration>
23+
<mainClass>com.baeldung.cxf.jaxrs.implementation.RestfulServer</mainClass>
24+
</configuration>
25+
</plugin>
26+
<plugin>
27+
<artifactId>maven-surefire-plugin</artifactId>
28+
<version>2.19.1</version>
29+
<configuration>
30+
<excludes>
31+
<exclude>**/ServiceTest</exclude>
32+
</excludes>
33+
</configuration>
34+
</plugin>
35+
</plugins>
36+
</build>
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.apache.cxf</groupId>
40+
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
41+
<version>${cxf.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.apache.cxf</groupId>
45+
<artifactId>cxf-rt-transports-http-jetty</artifactId>
46+
<version>${cxf.version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.apache.httpcomponents</groupId>
50+
<artifactId>httpclient</artifactId>
51+
<version>${httpclient.version}</version>
52+
</dependency>
53+
</dependencies>
54+
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.cxf.jaxrs.implementation;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import javax.ws.rs.GET;
9+
import javax.ws.rs.PUT;
10+
import javax.ws.rs.Path;
11+
import javax.ws.rs.PathParam;
12+
import javax.ws.rs.Produces;
13+
import javax.ws.rs.core.Response;
14+
15+
@Path("baeldung")
16+
@Produces("text/xml")
17+
public class Baeldung {
18+
private Map<Integer, Course> courses = new HashMap<>();
19+
20+
{
21+
Student student1 = new Student();
22+
Student student2 = new Student();
23+
student1.setId(1);
24+
student1.setName("Student A");
25+
student2.setId(2);
26+
student2.setName("Student B");
27+
28+
List<Student> course1Students = new ArrayList<>();
29+
course1Students.add(student1);
30+
course1Students.add(student2);
31+
32+
Course course1 = new Course();
33+
Course course2 = new Course();
34+
course1.setId(1);
35+
course1.setName("REST with Spring");
36+
course1.setStudents(course1Students);
37+
course2.setId(2);
38+
course2.setName("Learn Spring Security");
39+
40+
courses.put(1, course1);
41+
courses.put(2, course2);
42+
}
43+
44+
@GET
45+
@Path("courses/{courseOrder}")
46+
public Course getCourse(@PathParam("courseOrder") int courseOrder) {
47+
return courses.get(courseOrder);
48+
}
49+
50+
@PUT
51+
@Path("courses/{courseOrder}")
52+
public Response putCourse(@PathParam("courseOrder") int courseOrder, Course course) {
53+
Course existingCourse = courses.get(courseOrder);
54+
Response response;
55+
if (existingCourse == null || existingCourse.getId() != course.getId() || !(existingCourse.getName().equals(course.getName()))) {
56+
courses.put(courseOrder, course);
57+
response = Response.ok().build();
58+
} else {
59+
response = Response.notModified().build();
60+
}
61+
return response;
62+
}
63+
64+
@Path("courses/{courseOrder}/students")
65+
public Course pathToStudent(@PathParam("courseOrder") int courseOrder) {
66+
return courses.get(courseOrder);
67+
}
68+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.cxf.jaxrs.implementation;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.ws.rs.DELETE;
7+
import javax.ws.rs.GET;
8+
import javax.ws.rs.POST;
9+
import javax.ws.rs.Path;
10+
import javax.ws.rs.PathParam;
11+
import javax.ws.rs.core.Response;
12+
13+
import javax.xml.bind.annotation.XmlRootElement;
14+
15+
@XmlRootElement(name = "Course")
16+
public class Course {
17+
private int id;
18+
private String name;
19+
private List<Student> students;
20+
21+
public int getId() {
22+
return id;
23+
}
24+
25+
public void setId(int id) {
26+
this.id = id;
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public List<Student> getStudents() {
38+
return students;
39+
}
40+
41+
public void setStudents(List<Student> students) {
42+
this.students = students;
43+
}
44+
45+
@GET
46+
@Path("{studentOrder}")
47+
public Student getStudent(@PathParam("studentOrder")int studentOrder) {
48+
return students.get(studentOrder);
49+
}
50+
51+
@POST
52+
public Response postStudent(Student student) {
53+
if (students == null) {
54+
students = new ArrayList<Student>();
55+
}
56+
students.add(student);
57+
return Response.ok(student).build();
58+
}
59+
60+
@DELETE
61+
@Path("{studentOrder}")
62+
public Response deleteStudent(@PathParam("studentOrder") int studentOrder) {
63+
Student student = students.get(studentOrder);
64+
Response response;
65+
if (student != null) {
66+
students.remove(studentOrder);
67+
response = Response.ok().build();
68+
} else {
69+
response = Response.notModified().build();
70+
}
71+
return response;
72+
}
73+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.cxf.jaxrs.implementation;
2+
3+
import org.apache.cxf.endpoint.Server;
4+
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
5+
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
6+
7+
public class RestfulServer {
8+
public static void main(String args[]) throws Exception {
9+
JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
10+
factoryBean.setResourceClasses(Baeldung.class);
11+
factoryBean.setResourceProvider(new SingletonResourceProvider(new Baeldung()));
12+
factoryBean.setAddress("http://localhost:8080/");
13+
Server server = factoryBean.create();
14+
15+
System.out.println("Server ready...");
16+
Thread.sleep(60 * 1000);
17+
System.out.println("Server exiting");
18+
server.destroy();
19+
System.exit(0);
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.cxf.jaxrs.implementation;
2+
3+
import javax.xml.bind.annotation.XmlRootElement;
4+
5+
@XmlRootElement(name = "Student")
6+
public class Student {
7+
private int id;
8+
private String name;
9+
10+
public int getId() {
11+
return id;
12+
}
13+
14+
public void setId(int id) {
15+
this.id = id;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<Course>
2+
<id>3</id>
3+
<name>Apache CXF Support for RESTful</name>
4+
</Course>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
<Student>
3+
<id>3</id>
4+
<name>Student C</name>
5+
</Student>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.baeldung.cxf.jaxrs.implementation;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.AfterClass;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.io.InputStreamReader;
12+
import java.net.URL;
13+
14+
import javax.xml.bind.JAXB;
15+
16+
import org.apache.http.HttpResponse;
17+
import org.apache.http.client.methods.HttpDelete;
18+
import org.apache.http.client.methods.HttpPost;
19+
import org.apache.http.client.methods.HttpPut;
20+
import org.apache.http.entity.InputStreamEntity;
21+
import org.apache.http.impl.client.CloseableHttpClient;
22+
import org.apache.http.impl.client.HttpClients;
23+
24+
public class ServiceTest {
25+
private static final String BASE_URL = "http://localhost:8080/baeldung/courses/";
26+
private static CloseableHttpClient client;
27+
28+
@BeforeClass
29+
public static void createClient() {
30+
client = HttpClients.createDefault();
31+
}
32+
33+
@AfterClass
34+
public static void closeClient() throws IOException {
35+
client.close();
36+
}
37+
38+
@Test
39+
public void whenPutCourse_thenCorrect() throws IOException {
40+
HttpPut httpPut = new HttpPut(BASE_URL + "3");
41+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("course.xml");
42+
httpPut.setEntity(new InputStreamEntity(resourceStream));
43+
httpPut.setHeader("Content-Type", "text/xml");
44+
45+
HttpResponse response = client.execute(httpPut);
46+
assertEquals(200, response.getStatusLine().getStatusCode());
47+
48+
Course course = getCourse(3);
49+
assertEquals(3, course.getId());
50+
assertEquals("Apache CXF Support for RESTful", course.getName());
51+
}
52+
53+
@Test
54+
public void whenPostStudent_thenCorrect() throws IOException {
55+
HttpPost httpPost = new HttpPost(BASE_URL + "2/students");
56+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("student.xml");
57+
httpPost.setEntity(new InputStreamEntity(resourceStream));
58+
httpPost.setHeader("Content-Type", "text/xml");
59+
60+
HttpResponse response = client.execute(httpPost);
61+
assertEquals(200, response.getStatusLine().getStatusCode());
62+
63+
Student student = getStudent(2, 0);
64+
assertEquals(3, student.getId());
65+
assertEquals("Student C", student.getName());
66+
}
67+
68+
@Test
69+
public void whenDeleteStudent_thenCorrect() throws IOException {
70+
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/0");
71+
HttpResponse response = client.execute(httpDelete);
72+
assertEquals(200, response.getStatusLine().getStatusCode());
73+
74+
Course course = getCourse(1);
75+
assertEquals(1, course.getStudents().size());
76+
assertEquals(2, course.getStudents().get(0).getId());
77+
assertEquals("Student B", course.getStudents().get(0).getName());
78+
}
79+
80+
private Course getCourse(int courseOrder) throws IOException {
81+
URL url = new URL(BASE_URL + courseOrder);
82+
InputStream input = url.openStream();
83+
Course course = JAXB.unmarshal(new InputStreamReader(input), Course.class);
84+
return course;
85+
}
86+
87+
private Student getStudent(int courseOrder, int studentOrder) throws IOException {
88+
URL url = new URL(BASE_URL + courseOrder + "/students/" + studentOrder);
89+
InputStream input = url.openStream();
90+
Student student = JAXB.unmarshal(new InputStreamReader(input), Student.class);
91+
return student;
92+
}
93+
}

apache-cxf/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<modules>
99
<module>cxf-introduction</module>
1010
<module>cxf-spring</module>
11+
<module>cxf-jaxrs-implementation</module>
1112
</modules>
1213
<dependencies>
1314
<dependency>

0 commit comments

Comments
 (0)