Refactor REST with CXF example

This commit is contained in:
Grzegorz Piwowarek 2016-10-13 07:22:11 +02:00
parent e92dbccb78
commit 6a7e380efd
3 changed files with 23 additions and 39 deletions

View File

@ -1,17 +1,12 @@
package com.baeldung.cxf.jaxrs.implementation;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
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 {
@ -51,14 +46,13 @@ public class Baeldung {
@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.ok().build();
}
return response;
return Response.notModified().build();
}
@Path("courses/{courseOrder}/students")

View File

@ -1,17 +1,11 @@
package com.baeldung.cxf.jaxrs.implementation;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlRootElement;
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;
@ -51,7 +45,7 @@ public class Course {
@POST
public Response postStudent(Student student) {
if (students == null) {
students = new ArrayList<Student>();
students = new ArrayList<>();
}
students.add(student);
return Response.ok(student).build();

View File

@ -1,18 +1,5 @@
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;
@ -20,6 +7,17 @@ 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;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.xml.bind.JAXB;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import static org.junit.Assert.assertEquals;
public class ServiceTest {
private static final String BASE_URL = "http://localhost:8080/baeldung/courses/";
@ -80,14 +78,12 @@ public class ServiceTest {
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;
return JAXB.unmarshal(new InputStreamReader(input), Course.class);
}
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;
return JAXB.unmarshal(new InputStreamReader(input), Student.class);
}
}