Skip to content

Commit 6a7e380

Browse files
author
Grzegorz Piwowarek
committed
Refactor REST with CXF example
1 parent e92dbcc commit 6a7e380

3 files changed

Lines changed: 21 additions & 37 deletions

File tree

‎apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Baeldung.java‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
package com.baeldung.cxf.jaxrs.implementation;
22

3+
import javax.ws.rs.*;
4+
import javax.ws.rs.core.Response;
35
import java.util.ArrayList;
46
import java.util.HashMap;
57
import java.util.List;
68
import java.util.Map;
79

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-
1510
@Path("baeldung")
1611
@Produces("text/xml")
1712
public class Baeldung {
@@ -51,14 +46,13 @@ public Course getCourse(@PathParam("courseOrder") int courseOrder) {
5146
@Path("courses/{courseOrder}")
5247
public Response putCourse(@PathParam("courseOrder") int courseOrder, Course course) {
5348
Course existingCourse = courses.get(courseOrder);
54-
Response response;
49+
5550
if (existingCourse == null || existingCourse.getId() != course.getId() || !(existingCourse.getName().equals(course.getName()))) {
5651
courses.put(courseOrder, course);
57-
response = Response.ok().build();
58-
} else {
59-
response = Response.notModified().build();
52+
return Response.ok().build();
6053
}
61-
return response;
54+
55+
return Response.notModified().build();
6256
}
6357

6458
@Path("courses/{courseOrder}/students")

‎apache-cxf/cxf-jaxrs-implementation/src/main/java/com/baeldung/cxf/jaxrs/implementation/Course.java‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
package com.baeldung.cxf.jaxrs.implementation;
22

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;
3+
import javax.ws.rs.*;
114
import javax.ws.rs.core.Response;
12-
135
import javax.xml.bind.annotation.XmlRootElement;
6+
import java.util.ArrayList;
7+
import java.util.List;
148

159
@XmlRootElement(name = "Course")
1610
public class Course {
@@ -51,7 +45,7 @@ public Student getStudent(@PathParam("studentOrder")int studentOrder) {
5145
@POST
5246
public Response postStudent(Student student) {
5347
if (students == null) {
54-
students = new ArrayList<Student>();
48+
students = new ArrayList<>();
5549
}
5650
students.add(student);
5751
return Response.ok(student).build();

‎apache-cxf/cxf-jaxrs-implementation/src/test/java/com/baeldung/cxf/jaxrs/implementation/ServiceTest.java‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
package com.baeldung.cxf.jaxrs.implementation;
22

3-
import static org.junit.Assert.assertEquals;
4-
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;
510
import org.junit.AfterClass;
611
import org.junit.BeforeClass;
712
import org.junit.Test;
813

14+
import javax.xml.bind.JAXB;
915
import java.io.IOException;
1016
import java.io.InputStream;
1117
import java.io.InputStreamReader;
1218
import java.net.URL;
1319

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;
20+
import static org.junit.Assert.assertEquals;
2321

2422
public class ServiceTest {
2523
private static final String BASE_URL = "http://localhost:8080/baeldung/courses/";
@@ -80,14 +78,12 @@ public void whenDeleteStudent_thenCorrect() throws IOException {
8078
private Course getCourse(int courseOrder) throws IOException {
8179
URL url = new URL(BASE_URL + courseOrder);
8280
InputStream input = url.openStream();
83-
Course course = JAXB.unmarshal(new InputStreamReader(input), Course.class);
84-
return course;
81+
return JAXB.unmarshal(new InputStreamReader(input), Course.class);
8582
}
8683

8784
private Student getStudent(int courseOrder, int studentOrder) throws IOException {
8885
URL url = new URL(BASE_URL + courseOrder + "/students/" + studentOrder);
8986
InputStream input = url.openStream();
90-
Student student = JAXB.unmarshal(new InputStreamReader(input), Student.class);
91-
return student;
87+
return JAXB.unmarshal(new InputStreamReader(input), Student.class);
9288
}
9389
}

0 commit comments

Comments
 (0)