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:
parent
a5978cf259
commit
06ceb4d87c
1
pom.xml
1
pom.xml
|
@ -117,6 +117,7 @@
|
||||||
<module>spring-hibernate3</module>
|
<module>spring-hibernate3</module>
|
||||||
<module>spring-hibernate4</module>
|
<module>spring-hibernate4</module>
|
||||||
<module>spring-integration</module>
|
<module>spring-integration</module>
|
||||||
|
<module>spring-jersey</module>
|
||||||
<module>spring-jms</module>
|
<module>spring-jms</module>
|
||||||
<module>spring-jooq</module>
|
<module>spring-jooq</module>
|
||||||
<module>spring-jpa</module>
|
<module>spring-jpa</module>
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
*.class
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/neoDb*
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
|
@ -0,0 +1,3 @@
|
||||||
|
=========
|
||||||
|
|
||||||
|
## REST API with Jersey & Spring Example Project
|
|
@ -0,0 +1,210 @@
|
||||||
|
<?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/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>jersey-api</artifactId>
|
||||||
|
<version>0.1-SNAPSHOT</version>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<jersey.version>2.25</jersey.version>
|
||||||
|
<jcl.slf4j.version>1.7.22</jcl.slf4j.version>
|
||||||
|
<logback.version>1.1.8</logback.version>
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
|
<maven-war-plugin.version>3.0.0</maven-war-plugin.version>
|
||||||
|
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||||
|
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
|
<httpcore.version>4.4.5</httpcore.version>
|
||||||
|
<httpclient.version>4.5.2</httpclient.version>
|
||||||
|
<servlet-api-version>3.1.0</servlet-api-version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<version>${maven-war-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.cargo</groupId>
|
||||||
|
<artifactId>cargo-maven2-plugin</artifactId>
|
||||||
|
<version>${cargo-maven2-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<wait>true</wait>
|
||||||
|
<container>
|
||||||
|
<containerId>jetty8x</containerId>
|
||||||
|
<type>embedded</type>
|
||||||
|
</container>
|
||||||
|
<configuration>
|
||||||
|
<properties>
|
||||||
|
<cargo.servlet.port>8082</cargo.servlet.port>
|
||||||
|
</properties>
|
||||||
|
</configuration>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- core library -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.jersey.core</groupId>
|
||||||
|
<artifactId>jersey-server</artifactId>
|
||||||
|
<version>${jersey.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.jersey.containers</groupId>
|
||||||
|
<artifactId>jersey-container-servlet</artifactId>
|
||||||
|
<version>${jersey.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.jersey.media</groupId>
|
||||||
|
<artifactId>jersey-media-json-jackson</artifactId>
|
||||||
|
<version>${jersey.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- servlet api -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
<version>${servlet-api-version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- optional library -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.jersey.ext</groupId>
|
||||||
|
<artifactId>jersey-spring3</artifactId>
|
||||||
|
<version>${jersey.version}</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>commons-logging</groupId>
|
||||||
|
<artifactId>commons-logging</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>jcl-over-slf4j</artifactId>
|
||||||
|
<version>${jcl.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-classic</artifactId>
|
||||||
|
<version>${logback.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- http client -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<version>${httpclient.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpcore</artifactId>
|
||||||
|
<version>${httpcore.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- junit -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>live</id>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*IntegrationTest.java</exclude>
|
||||||
|
</excludes>
|
||||||
|
<includes>
|
||||||
|
<include>**/*LiveTest.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<test.mime>json</test.mime>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.cargo</groupId>
|
||||||
|
<artifactId>cargo-maven2-plugin</artifactId>
|
||||||
|
<version>${cargo-maven2-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<wait>false</wait>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>start-server</id>
|
||||||
|
<phase>pre-integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>start</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>stop-server</id>
|
||||||
|
<phase>post-integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>stop</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
</project>
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
<configuration>
|
||||||
|
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>web - %date [%thread] %-5level %logger{36} -
|
||||||
|
%message%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
|
||||||
|
</configuration>
|
|
@ -0,0 +1,91 @@
|
||||||
|
package com.baeldung.server;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.HttpStatus;
|
||||||
|
import org.apache.http.client.ClientProtocolException;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.client.methods.HttpUriRequest;
|
||||||
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.server.model.Employee;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
public class JerseyApiLiveTest {
|
||||||
|
|
||||||
|
private static final String SERVICE_URL = "http://localhost:8082/jersey-api/resources/employees";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGetAllEmployees_whenCorrectRequest_thenResponseCodeSuccess() throws ClientProtocolException, IOException {
|
||||||
|
final HttpUriRequest request = new HttpGet(SERVICE_URL);
|
||||||
|
|
||||||
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
||||||
|
|
||||||
|
assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGetEmployee_whenEmployeeExists_thenResponseCodeSuccess() throws ClientProtocolException, IOException {
|
||||||
|
final HttpUriRequest request = new HttpGet(SERVICE_URL + "/1");
|
||||||
|
|
||||||
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
||||||
|
|
||||||
|
assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGetEmployee_whenEmployeeDoesNotExist_thenResponseCodeNotFound() throws ClientProtocolException, IOException {
|
||||||
|
final HttpUriRequest request = new HttpGet(SERVICE_URL + "/1000");
|
||||||
|
|
||||||
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
||||||
|
|
||||||
|
assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGetEmployee_whenJsonRequested_thenCorrectDataRetrieved() throws ClientProtocolException, IOException {
|
||||||
|
final HttpUriRequest request = new HttpGet(SERVICE_URL + "/1");
|
||||||
|
|
||||||
|
request.setHeader("Accept", "application/json");
|
||||||
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
Employee emp = mapper.readValue(httpResponse.getEntity().getContent(), Employee.class);
|
||||||
|
|
||||||
|
assert(emp.getFirstName().equals("Jane"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAddEmployee_whenJsonRequestSent_thenResponseCodeCreated() throws ClientProtocolException, IOException {
|
||||||
|
final HttpPost request = new HttpPost(SERVICE_URL);
|
||||||
|
|
||||||
|
Employee emp = new Employee(5, "Johny");
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
String empJson = mapper.writeValueAsString(emp);
|
||||||
|
StringEntity input = new StringEntity(empJson);
|
||||||
|
input.setContentType("application/json");
|
||||||
|
request.setEntity(input);
|
||||||
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
||||||
|
|
||||||
|
assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAddEmployee_whenRequestForExistingObjectSent_thenResponseCodeConflict() throws ClientProtocolException, IOException {
|
||||||
|
final HttpPost request = new HttpPost(SERVICE_URL);
|
||||||
|
|
||||||
|
Employee emp = new Employee(1, "Johny");
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
String empJson = mapper.writeValueAsString(emp);
|
||||||
|
StringEntity input = new StringEntity(empJson);
|
||||||
|
input.setContentType("application/json");
|
||||||
|
request.setEntity(input);
|
||||||
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
||||||
|
|
||||||
|
assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_CONFLICT);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue