forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
344 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>cxf-jaxrs-implementation</artifactId> | ||
<parent> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>apache-cxf</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<cxf.version>3.1.7</cxf.version> | ||
<httpclient.version>4.5.2</httpclient.version> | ||
</properties> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<configuration> | ||
<mainClass>com.baeldung.cxf.jaxrs.implementation.RestfulServer</mainClass> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.19.1</version> | ||
<configuration> | ||
<excludes> | ||
<exclude>**/ServiceTest</exclude> | ||
</excludes> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.cxf</groupId> | ||
<artifactId>cxf-rt-frontend-jaxrs</artifactId> | ||
<version>${cxf.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.cxf</groupId> | ||
<artifactId>cxf-rt-transports-http-jetty</artifactId> | ||
<version>${cxf.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>${httpclient.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
68 changes: 68 additions & 0 deletions
68
...xf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.baeldung.cxf.jaxrs.implementation; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.PUT; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Response; | ||
|
||
@Path("baeldung") | ||
@Produces("text/xml") | ||
public class Baeldung { | ||
private Map<Integer, Course> courses = new HashMap<>(); | ||
|
||
{ | ||
Student student1 = new Student(); | ||
Student student2 = new Student(); | ||
student1.setId(1); | ||
student1.setName("Student A"); | ||
student2.setId(2); | ||
student2.setName("Student B"); | ||
|
||
List<Student> course1Students = new ArrayList<>(); | ||
course1Students.add(student1); | ||
course1Students.add(student2); | ||
|
||
Course course1 = new Course(); | ||
Course course2 = new Course(); | ||
course1.setId(1); | ||
course1.setName("REST with Spring"); | ||
course1.setStudents(course1Students); | ||
course2.setId(2); | ||
course2.setName("Learn Spring Security"); | ||
|
||
courses.put(1, course1); | ||
courses.put(2, course2); | ||
} | ||
|
||
@GET | ||
@Path("courses/{courseOrder}") | ||
public Course getCourse(@PathParam("courseOrder") int courseOrder) { | ||
return courses.get(courseOrder); | ||
} | ||
|
||
@PUT | ||
@Path("courses/{courseOrder}") | ||
public Response putCourse(@PathParam("courseOrder") int courseOrder, Course course) { | ||
Course existingCourse = courses.get(courseOrder); | ||
Response response; | ||
if (existingCourse == null || existingCourse.getId() != course.getId() || !(existingCourse.getName().equals(course.getName()))) { | ||
courses.put(courseOrder, course); | ||
response = Response.ok().build(); | ||
} else { | ||
response = Response.notModified().build(); | ||
} | ||
return response; | ||
} | ||
|
||
@Path("courses/{courseOrder}/students") | ||
public Course pathToStudent(@PathParam("courseOrder") int courseOrder) { | ||
return courses.get(courseOrder); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
.../cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.baeldung.cxf.jaxrs.implementation; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.ws.rs.DELETE; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.core.Response; | ||
|
||
import javax.xml.bind.annotation.XmlRootElement; | ||
|
||
@XmlRootElement(name = "Course") | ||
public class Course { | ||
private int id; | ||
private String name; | ||
private List<Student> students; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public List<Student> getStudents() { | ||
return students; | ||
} | ||
|
||
public void setStudents(List<Student> students) { | ||
this.students = students; | ||
} | ||
|
||
@GET | ||
@Path("{studentOrder}") | ||
public Student getStudent(@PathParam("studentOrder")int studentOrder) { | ||
return students.get(studentOrder); | ||
} | ||
|
||
@POST | ||
public Response postStudent(Student student) { | ||
if (students == null) { | ||
students = new ArrayList<Student>(); | ||
} | ||
students.add(student); | ||
return Response.ok(student).build(); | ||
} | ||
|
||
@DELETE | ||
@Path("{studentOrder}") | ||
public Response deleteStudent(@PathParam("studentOrder") int studentOrder) { | ||
Student student = students.get(studentOrder); | ||
Response response; | ||
if (student != null) { | ||
students.remove(studentOrder); | ||
response = Response.ok().build(); | ||
} else { | ||
response = Response.notModified().build(); | ||
} | ||
return response; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...xrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/RestfulServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.baeldung.cxf.jaxrs.implementation; | ||
|
||
import org.apache.cxf.endpoint.Server; | ||
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; | ||
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider; | ||
|
||
public class RestfulServer { | ||
public static void main(String args[]) throws Exception { | ||
JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean(); | ||
factoryBean.setResourceClasses(Baeldung.class); | ||
factoryBean.setResourceProvider(new SingletonResourceProvider(new Baeldung())); | ||
factoryBean.setAddress("http://localhost:8080/"); | ||
Server server = factoryBean.create(); | ||
|
||
System.out.println("Server ready..."); | ||
Thread.sleep(60 * 1000); | ||
System.out.println("Server exiting"); | ||
server.destroy(); | ||
System.exit(0); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.baeldung.cxf.jaxrs.implementation; | ||
|
||
import javax.xml.bind.annotation.XmlRootElement; | ||
|
||
@XmlRootElement(name = "Student") | ||
public class Student { | ||
private int id; | ||
private String name; | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
apache-cxf/cxf-jaxrs-implementation/src/main/resources/course.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<Course> | ||
<id>3</id> | ||
<name>Apache CXF Support for RESTful</name> | ||
</Course> |
5 changes: 5 additions & 0 deletions
5
apache-cxf/cxf-jaxrs-implementation/src/main/resources/student.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
<Student> | ||
<id>3</id> | ||
<name>Student C</name> | ||
</Student> |
93 changes: 93 additions & 0 deletions
93
...jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.baeldung.cxf.jaxrs.implementation; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
|
||
import javax.xml.bind.JAXB; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.methods.HttpDelete; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.client.methods.HttpPut; | ||
import org.apache.http.entity.InputStreamEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
|
||
public class ServiceTest { | ||
private static final String BASE_URL = "http://localhost:8080/baeldung/courses/"; | ||
private static CloseableHttpClient client; | ||
|
||
@BeforeClass | ||
public static void createClient() { | ||
client = HttpClients.createDefault(); | ||
} | ||
|
||
@AfterClass | ||
public static void closeClient() throws IOException { | ||
client.close(); | ||
} | ||
|
||
@Test | ||
public void whenPutCourse_thenCorrect() throws IOException { | ||
HttpPut httpPut = new HttpPut(BASE_URL + "3"); | ||
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("course.xml"); | ||
httpPut.setEntity(new InputStreamEntity(resourceStream)); | ||
httpPut.setHeader("Content-Type", "text/xml"); | ||
|
||
HttpResponse response = client.execute(httpPut); | ||
assertEquals(200, response.getStatusLine().getStatusCode()); | ||
|
||
Course course = getCourse(3); | ||
assertEquals(3, course.getId()); | ||
assertEquals("Apache CXF Support for RESTful", course.getName()); | ||
} | ||
|
||
@Test | ||
public void whenPostStudent_thenCorrect() throws IOException { | ||
HttpPost httpPost = new HttpPost(BASE_URL + "2/students"); | ||
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("student.xml"); | ||
httpPost.setEntity(new InputStreamEntity(resourceStream)); | ||
httpPost.setHeader("Content-Type", "text/xml"); | ||
|
||
HttpResponse response = client.execute(httpPost); | ||
assertEquals(200, response.getStatusLine().getStatusCode()); | ||
|
||
Student student = getStudent(2, 0); | ||
assertEquals(3, student.getId()); | ||
assertEquals("Student C", student.getName()); | ||
} | ||
|
||
@Test | ||
public void whenDeleteStudent_thenCorrect() throws IOException { | ||
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/0"); | ||
HttpResponse response = client.execute(httpDelete); | ||
assertEquals(200, response.getStatusLine().getStatusCode()); | ||
|
||
Course course = getCourse(1); | ||
assertEquals(1, course.getStudents().size()); | ||
assertEquals(2, course.getStudents().get(0).getId()); | ||
assertEquals("Student B", course.getStudents().get(0).getName()); | ||
} | ||
|
||
private Course getCourse(int courseOrder) throws IOException { | ||
URL url = new URL(BASE_URL + courseOrder); | ||
InputStream input = url.openStream(); | ||
Course course = JAXB.unmarshal(new InputStreamReader(input), Course.class); | ||
return course; | ||
} | ||
|
||
private Student getStudent(int courseOrder, int studentOrder) throws IOException { | ||
URL url = new URL(BASE_URL + courseOrder + "/students/" + studentOrder); | ||
InputStream input = url.openStream(); | ||
Student student = JAXB.unmarshal(new InputStreamReader(input), Student.class); | ||
return student; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters