Renamed Baeldung class to CourseRepository class

This commit is contained in:
Zeger Hendrikse 2016-10-25 17:50:33 +02:00
parent 54572b9c9d
commit 084d772bbd
2 changed files with 3 additions and 75 deletions

View File

@ -1,72 +0,0 @@
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;
@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/{courseId}")
public Course getCourse(@PathParam("courseId") int courseId) {
return findById(courseId);
}
@PUT
@Path("courses/{courseId}")
public Response updateCourse(@PathParam("courseId") int courseId, Course course) {
Course existingCourse = findById(courseId);
if (existingCourse == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
if (existingCourse.equals(course)) {
return Response.notModified().build();
}
courses.put(courseId, course);
return Response.ok().build();
}
@Path("courses/{courseId}/students")
public Course pathToStudent(@PathParam("courseId") int courseId) {
return findById(courseId);
}
private Course findById(int id) {
for (Map.Entry<Integer, Course> course : courses.entrySet()) {
if (course.getKey() == id) {
return course.getValue();
}
}
return null;
}
}

View File

@ -7,8 +7,8 @@ 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.setResourceClasses(CourseRepository.class);
factoryBean.setResourceProvider(new SingletonResourceProvider(new CourseRepository()));
factoryBean.setAddress("http://localhost:8080/");
Server server = factoryBean.create();
@ -18,4 +18,4 @@ public class RestfulServer {
server.destroy();
System.exit(0);
}
}
}