Skip to content

Commit a74e46d

Browse files
fix conflicts
2 parents 7ad9752 + 6a7e380 commit a74e46d

390 files changed

Lines changed: 8043 additions & 6539 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎annotations/readme.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.cxf.jaxrs.implementation;
2+
3+
import javax.ws.rs.*;
4+
import javax.ws.rs.core.Response;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
@Path("baeldung")
11+
@Produces("text/xml")
12+
public class Baeldung {
13+
private Map<Integer, Course> courses = new HashMap<>();
14+
15+
{
16+
Student student1 = new Student();
17+
Student student2 = new Student();
18+
student1.setId(1);
19+
student1.setName("Student A");
20+
student2.setId(2);
21+
student2.setName("Student B");
22+
23+
List<Student> course1Students = new ArrayList<>();
24+
course1Students.add(student1);
25+
course1Students.add(student2);
26+
27+
Course course1 = new Course();
28+
Course course2 = new Course();
29+
course1.setId(1);
30+
course1.setName("REST with Spring");
31+
course1.setStudents(course1Students);
32+
course2.setId(2);
33+
course2.setName("Learn Spring Security");
34+
35+
courses.put(1, course1);
36+
courses.put(2, course2);
37+
}
38+
39+
@GET
40+
@Path("courses/{courseOrder}")
41+
public Course getCourse(@PathParam("courseOrder") int courseOrder) {
42+
return courses.get(courseOrder);
43+
}
44+
45+
@PUT
46+
@Path("courses/{courseOrder}")
47+
public Response putCourse(@PathParam("courseOrder") int courseOrder, Course course) {
48+
Course existingCourse = courses.get(courseOrder);
49+
50+
if (existingCourse == null || existingCourse.getId() != course.getId() || !(existingCourse.getName().equals(course.getName()))) {
51+
courses.put(courseOrder, course);
52+
return Response.ok().build();
53+
}
54+
55+
return Response.notModified().build();
56+
}
57+
58+
@Path("courses/{courseOrder}/students")
59+
public Course pathToStudent(@PathParam("courseOrder") int courseOrder) {
60+
return courses.get(courseOrder);
61+
}
62+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.cxf.jaxrs.implementation;
2+
3+
import javax.ws.rs.*;
4+
import javax.ws.rs.core.Response;
5+
import javax.xml.bind.annotation.XmlRootElement;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
@XmlRootElement(name = "Course")
10+
public class Course {
11+
private int id;
12+
private String name;
13+
private List<Student> students;
14+
15+
public int getId() {
16+
return id;
17+
}
18+
19+
public void setId(int id) {
20+
this.id = id;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
31+
public List<Student> getStudents() {
32+
return students;
33+
}
34+
35+
public void setStudents(List<Student> students) {
36+
this.students = students;
37+
}
38+
39+
@GET
40+
@Path("{studentOrder}")
41+
public Student getStudent(@PathParam("studentOrder")int studentOrder) {
42+
return students.get(studentOrder);
43+
}
44+
45+
@POST
46+
public Response postStudent(Student student) {
47+
if (students == null) {
48+
students = new ArrayList<>();
49+
}
50+
students.add(student);
51+
return Response.ok(student).build();
52+
}
53+
54+
@DELETE
55+
@Path("{studentOrder}")
56+
public Response deleteStudent(@PathParam("studentOrder") int studentOrder) {
57+
Student student = students.get(studentOrder);
58+
if (student != null) {
59+
students.remove(studentOrder);
60+
return Response.ok().build();
61+
} else {
62+
return Response.notModified().build();
63+
}
64+
}
65+
}
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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.baeldung.cxf.jaxrs.implementation;
2+
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.client.methods.HttpDelete;
5+
import org.apache.http.client.methods.HttpPost;
6+
import org.apache.http.client.methods.HttpPut;
7+
import org.apache.http.entity.InputStreamEntity;
8+
import org.apache.http.impl.client.CloseableHttpClient;
9+
import org.apache.http.impl.client.HttpClients;
10+
import org.junit.AfterClass;
11+
import org.junit.BeforeClass;
12+
import org.junit.Test;
13+
14+
import javax.xml.bind.JAXB;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.io.InputStreamReader;
18+
import java.net.URL;
19+
20+
import static org.junit.Assert.assertEquals;
21+
22+
public class ServiceTest {
23+
private static final String BASE_URL = "http://localhost:8080/baeldung/courses/";
24+
private static CloseableHttpClient client;
25+
26+
@BeforeClass
27+
public static void createClient() {
28+
client = HttpClients.createDefault();
29+
}
30+
31+
@AfterClass
32+
public static void closeClient() throws IOException {
33+
client.close();
34+
}
35+
36+
@Test
37+
public void whenPutCourse_thenCorrect() throws IOException {
38+
HttpPut httpPut = new HttpPut(BASE_URL + "3");
39+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("course.xml");
40+
httpPut.setEntity(new InputStreamEntity(resourceStream));
41+
httpPut.setHeader("Content-Type", "text/xml");
42+
43+
HttpResponse response = client.execute(httpPut);
44+
assertEquals(200, response.getStatusLine().getStatusCode());
45+
46+
Course course = getCourse(3);
47+
assertEquals(3, course.getId());
48+
assertEquals("Apache CXF Support for RESTful", course.getName());
49+
}
50+
51+
@Test
52+
public void whenPostStudent_thenCorrect() throws IOException {
53+
HttpPost httpPost = new HttpPost(BASE_URL + "2/students");
54+
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("student.xml");
55+
httpPost.setEntity(new InputStreamEntity(resourceStream));
56+
httpPost.setHeader("Content-Type", "text/xml");
57+
58+
HttpResponse response = client.execute(httpPost);
59+
assertEquals(200, response.getStatusLine().getStatusCode());
60+
61+
Student student = getStudent(2, 0);
62+
assertEquals(3, student.getId());
63+
assertEquals("Student C", student.getName());
64+
}
65+
66+
@Test
67+
public void whenDeleteStudent_thenCorrect() throws IOException {
68+
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/0");
69+
HttpResponse response = client.execute(httpDelete);
70+
assertEquals(200, response.getStatusLine().getStatusCode());
71+
72+
Course course = getCourse(1);
73+
assertEquals(1, course.getStudents().size());
74+
assertEquals(2, course.getStudents().get(0).getId());
75+
assertEquals("Student B", course.getStudents().get(0).getName());
76+
}
77+
78+
private Course getCourse(int courseOrder) throws IOException {
79+
URL url = new URL(BASE_URL + courseOrder);
80+
InputStream input = url.openStream();
81+
return JAXB.unmarshal(new InputStreamReader(input), Course.class);
82+
}
83+
84+
private Student getStudent(int courseOrder, int studentOrder) throws IOException {
85+
URL url = new URL(BASE_URL + courseOrder + "/students/" + studentOrder);
86+
InputStream input = url.openStream();
87+
return JAXB.unmarshal(new InputStreamReader(input), Student.class);
88+
}
89+
}

‎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)