diff --git a/spring-jersey/pom.xml b/spring-jersey/pom.xml
index 00d67febec..293850d41e 100644
--- a/spring-jersey/pom.xml
+++ b/spring-jersey/pom.xml
@@ -4,7 +4,7 @@
4.0.0
com.baeldung
- jersey-api
+ spring-jersey
0.1-SNAPSHOT
war
@@ -23,6 +23,7 @@
+ spring-jersey
org.apache.maven.plugins
@@ -75,11 +76,6 @@
-
- org.glassfish.jersey.core
- jersey-server
- ${jersey.version}
-
org.glassfish.jersey.containers
jersey-container-servlet
@@ -90,6 +86,12 @@
jersey-media-json-jackson
${jersey.version}
+
+ org.glassfish.jersey.core
+ jersey-client
+ ${jersey.version}
+
+
javax.servlet
@@ -97,8 +99,8 @@
${servlet-api-version}
provided
-
+
org.glassfish.jersey.ext
jersey-spring3
diff --git a/spring-jersey/src/main/java/com/baeldung/client/rest/RestClient.java b/spring-jersey/src/main/java/com/baeldung/client/rest/RestClient.java
new file mode 100644
index 0000000000..0e45b68b14
--- /dev/null
+++ b/spring-jersey/src/main/java/com/baeldung/client/rest/RestClient.java
@@ -0,0 +1,31 @@
+package com.baeldung.client.rest;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import com.baeldung.server.model.Employee;
+
+public class RestClient {
+
+ private static final String REST_URI = "http://localhost:8082/spring-jersey/resources/employees";
+ private Client client = ClientBuilder.newClient();
+
+ public Response createJsonEmployee(Employee emp) {
+ return client.target(REST_URI).request(MediaType.APPLICATION_JSON).post(Entity.entity(emp, MediaType.APPLICATION_JSON));
+ }
+
+ public Employee getJsonEmployee(int id) {
+ return client.target(REST_URI).path(new Integer(id).toString()).request(MediaType.APPLICATION_JSON).get(Employee.class);
+ }
+
+ public Response createXmlEmployee(Employee emp) {
+ return client.target(REST_URI).request(MediaType.APPLICATION_XML).post(Entity.entity(emp, MediaType.APPLICATION_XML));
+ }
+
+ public Employee getXmlEmployee(int id) {
+ return client.target(REST_URI).path(new Integer(id).toString()).request(MediaType.APPLICATION_XML).get(Employee.class);
+ }
+}
diff --git a/spring-jersey/src/test/java/com/baeldung/client/JerseyClientLiveTest.java b/spring-jersey/src/test/java/com/baeldung/client/JerseyClientLiveTest.java
new file mode 100644
index 0000000000..be87cd5478
--- /dev/null
+++ b/spring-jersey/src/test/java/com/baeldung/client/JerseyClientLiveTest.java
@@ -0,0 +1,53 @@
+package com.baeldung.client;
+
+import static org.junit.Assert.assertEquals;
+
+import javax.ws.rs.core.Response;
+
+import org.junit.Test;
+
+import com.baeldung.client.rest.RestClient;
+import com.baeldung.server.model.Employee;
+
+public class JerseyClientLiveTest {
+
+ public static final int HTTP_CREATED = 201;
+
+ private RestClient client = new RestClient();
+
+ @Test
+ public void givenCorrectObject_whenCorrectJsonRequest_thenResponseCodeCreated() {
+ Employee emp = new Employee(6, "Johny");
+
+ Response response = client.createJsonEmployee(emp);
+
+ assertEquals(response.getStatus(), HTTP_CREATED);
+ }
+
+ @Test
+ public void givenCorrectObject_whenCorrectXmlRequest_thenResponseCodeCreated() {
+ Employee emp = new Employee(7, "Jacky");
+
+ Response response = client.createXmlEmployee(emp);
+
+ assertEquals(response.getStatus(), HTTP_CREATED);
+ }
+
+ @Test
+ public void givenCorrectId_whenCorrectJsonRequest_thenCorrectEmployeeRetrieved() {
+ int employeeId = 1;
+
+ Employee emp = client.getJsonEmployee(employeeId);
+
+ assertEquals(emp.getFirstName(), "Jane");
+ }
+
+ @Test
+ public void givenCorrectId_whenCorrectXmlRequest_thenCorrectEmployeeRetrieved() {
+ int employeeId = 1;
+
+ Employee emp = client.getXmlEmployee(employeeId);
+
+ assertEquals(emp.getFirstName(), "Jane");
+ }
+}
diff --git a/spring-jersey/src/test/java/com/baeldung/server/JerseyApiLiveTest.java b/spring-jersey/src/test/java/com/baeldung/server/JerseyApiLiveTest.java
index 835f9144ab..12de677180 100644
--- a/spring-jersey/src/test/java/com/baeldung/server/JerseyApiLiveTest.java
+++ b/spring-jersey/src/test/java/com/baeldung/server/JerseyApiLiveTest.java
@@ -1,5 +1,8 @@
package com.baeldung.server;
+
+import static org.junit.Assert.assertEquals;
+
import com.baeldung.server.model.Employee;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpResponse;
@@ -15,7 +18,7 @@ import java.io.IOException;
public class JerseyApiLiveTest {
- private static final String SERVICE_URL = "http://localhost:8082/jersey-api/resources/employees";
+ private static final String SERVICE_URL = "http://localhost:8082/spring-jersey/resources/employees";
@Test
public void givenGetAllEmployees_whenCorrectRequest_thenResponseCodeSuccess() throws IOException {
@@ -23,7 +26,7 @@ public class JerseyApiLiveTest {
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
- assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK);
+ assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_OK);
}
@Test
@@ -32,7 +35,7 @@ public class JerseyApiLiveTest {
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
- assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK);
+ assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_OK);
}
@Test
@@ -41,7 +44,7 @@ public class JerseyApiLiveTest {
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
- assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND);
+ assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_NOT_FOUND);
}
@Test
@@ -53,7 +56,7 @@ public class JerseyApiLiveTest {
ObjectMapper mapper = new ObjectMapper();
Employee emp = mapper.readValue(httpResponse.getEntity().getContent(), Employee.class);
- assert(emp.getFirstName().equals("Jane"));
+ assertEquals(emp.getFirstName(), "Jane");
}
@Test
@@ -68,7 +71,7 @@ public class JerseyApiLiveTest {
request.setEntity(input);
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
- assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED);
+ assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_CREATED);
}
@Test
@@ -83,7 +86,7 @@ public class JerseyApiLiveTest {
request.setEntity(input);
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
- assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_CONFLICT);
+ assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_CONFLICT);
}
}
\ No newline at end of file