JAX-RS API using Jersey [BAEL-558] (#956)
* WatchService vs. Apache Commons IO Mnitoring * Indentation fixed * Indentation fixed * JAX-RS API using Jersey [BAEL-558] * JAX-RS API using Jersey [BAEL-558] * Modifications made to remove xml * applicationContext.xml removed * All try catch moved to ExceptionMapper * fixes * review comments incorporated * module renamed
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
a5978cf259
commit
06ceb4d87c
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.server.config;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class ApplicationInitializer implements WebApplicationInitializer {
|
||||
|
||||
@Override
|
||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
|
||||
servletContext.addListener(new ContextLoaderListener(context));
|
||||
servletContext.setInitParameter("contextConfigLocation", "com.baeldung.server");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.server.config;
|
||||
|
||||
import javax.ws.rs.ApplicationPath;
|
||||
import javax.ws.rs.core.Application;
|
||||
|
||||
import com.baeldung.server.exception.AlreadyExistsExceptionHandler;
|
||||
import com.baeldung.server.exception.NotFoundExceptionHandler;
|
||||
import com.baeldung.server.rest.EmployeeResource;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@ApplicationPath("/resources")
|
||||
public class RestConfig extends Application {
|
||||
public Set<Class<?>> getClasses() {
|
||||
return new HashSet<Class<?>>(Arrays.asList(EmployeeResource.class, NotFoundExceptionHandler.class, AlreadyExistsExceptionHandler.class));
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.server.exception;
|
||||
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.ext.ExceptionMapper;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
|
||||
@Provider
|
||||
public class AlreadyExistsExceptionHandler implements ExceptionMapper<EmployeeAlreadyExists> {
|
||||
public Response toResponse(EmployeeAlreadyExists ex) {
|
||||
return Response.status(Response.Status.CONFLICT.getStatusCode()).build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.server.exception;
|
||||
|
||||
public class EmployeeAlreadyExists extends RuntimeException {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.server.exception;
|
||||
|
||||
public class EmployeeNotFound extends RuntimeException {
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.server.exception;
|
||||
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.ext.ExceptionMapper;
|
||||
import javax.ws.rs.ext.Provider;
|
||||
|
||||
@Provider
|
||||
public class NotFoundExceptionHandler implements ExceptionMapper<EmployeeNotFound> {
|
||||
public Response toResponse(EmployeeNotFound ex) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.server.model;
|
||||
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement
|
||||
public class Employee {
|
||||
private int id;
|
||||
private String firstName;
|
||||
|
||||
public Employee() {
|
||||
|
||||
}
|
||||
|
||||
public Employee(int id, String firstName) {
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.server.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.server.model.Employee;
|
||||
|
||||
public interface EmployeeRepository {
|
||||
|
||||
public List<Employee> getAllEmployees();
|
||||
|
||||
public Employee getEmployee(int id);
|
||||
|
||||
public void updateEmployee(Employee employee, int id);
|
||||
|
||||
public void deleteEmployee(int id);
|
||||
|
||||
public void addEmployee(Employee employee);
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.server.repository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.baeldung.server.exception.EmployeeAlreadyExists;
|
||||
import com.baeldung.server.exception.EmployeeNotFound;
|
||||
import com.baeldung.server.model.Employee;
|
||||
|
||||
@Component
|
||||
public class EmployeeRepositoryImpl implements EmployeeRepository {
|
||||
private List<Employee> employeeList;
|
||||
|
||||
public EmployeeRepositoryImpl() {
|
||||
employeeList = new ArrayList<Employee>();
|
||||
employeeList.add(new Employee(1, "Jane"));
|
||||
employeeList.add(new Employee(2, "Jack"));
|
||||
employeeList.add(new Employee(3, "George"));
|
||||
}
|
||||
|
||||
public List<Employee> getAllEmployees() {
|
||||
return employeeList;
|
||||
}
|
||||
|
||||
public Employee getEmployee(int id) {
|
||||
for (Employee emp : employeeList) {
|
||||
if (emp.getId() == id) {
|
||||
return emp;
|
||||
}
|
||||
}
|
||||
throw new EmployeeNotFound();
|
||||
}
|
||||
|
||||
public void updateEmployee(Employee employee, int id) {
|
||||
for (Employee emp : employeeList) {
|
||||
if (emp.getId() == id) {
|
||||
emp.setId(employee.getId());
|
||||
emp.setFirstName(employee.getFirstName());
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new EmployeeNotFound();
|
||||
}
|
||||
|
||||
public void deleteEmployee(int id) {
|
||||
for (Employee emp : employeeList) {
|
||||
if (emp.getId() == id) {
|
||||
employeeList.remove(emp);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new EmployeeNotFound();
|
||||
}
|
||||
|
||||
public void addEmployee(Employee employee) {
|
||||
for (Employee emp : employeeList) {
|
||||
if (emp.getId() == employee.getId()) {
|
||||
throw new EmployeeAlreadyExists();
|
||||
}
|
||||
}
|
||||
employeeList.add(employee);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.server.rest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
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.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.baeldung.server.model.Employee;
|
||||
import com.baeldung.server.repository.EmployeeRepository;
|
||||
|
||||
@Path("/employees")
|
||||
public class EmployeeResource {
|
||||
|
||||
@Autowired
|
||||
private EmployeeRepository employeeRepository;
|
||||
|
||||
@GET
|
||||
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||
public List<Employee> getAllEmployees() {
|
||||
return employeeRepository.getAllEmployees();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/{id}")
|
||||
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||
public Employee getEmployee(@PathParam("id") int id) {
|
||||
return employeeRepository.getEmployee(id);
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("/{id}")
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||
public Response updateEmployee(Employee employee, @PathParam("id") int id) {
|
||||
employeeRepository.updateEmployee(employee, id);
|
||||
return Response.status(Response.Status.OK.getStatusCode()).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/{id}")
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||
public Response deleteEmployee(@PathParam("id") int id) {
|
||||
employeeRepository.deleteEmployee(id);
|
||||
return Response.status(Response.Status.OK.getStatusCode()).build();
|
||||
}
|
||||
|
||||
@POST
|
||||
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||
public Response addEmployee(Employee employee, @Context UriInfo uriInfo) {
|
||||
employeeRepository.addEmployee(new Employee(employee.getId(), employee.getFirstName()));
|
||||
return Response.status(Response.Status.CREATED.getStatusCode()).header("Location", String.format("%s/%s", uriInfo.getAbsolutePath().toString(), employee.getId())).build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user