Skip to content

Commit 4fe878d

Browse files
saptarshibasusunilmogadati
authored andcommitted
JAX-RS Client [BAEL-595] (eugenp#992)
* 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 * JAX-RS client [BAEL-595] * jersey-core dependency removed * assert changed to assertEquals * messagebody readers and writers removed * pom dependency corrected and other minor changes
1 parent 6c2ed87 commit 4fe878d

4 files changed

Lines changed: 103 additions & 14 deletions

File tree

‎spring-jersey/pom.xml‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55

66
<groupId>com.baeldung</groupId>
7-
<artifactId>jersey-api</artifactId>
7+
<artifactId>spring-jersey</artifactId>
88
<version>0.1-SNAPSHOT</version>
99
<packaging>war</packaging>
1010

@@ -23,6 +23,7 @@
2323
</properties>
2424

2525
<build>
26+
<finalName>spring-jersey</finalName>
2627
<plugins>
2728
<plugin>
2829
<groupId>org.apache.maven.plugins</groupId>
@@ -75,11 +76,6 @@
7576

7677
<!-- core library -->
7778

78-
<dependency>
79-
<groupId>org.glassfish.jersey.core</groupId>
80-
<artifactId>jersey-server</artifactId>
81-
<version>${jersey.version}</version>
82-
</dependency>
8379
<dependency>
8480
<groupId>org.glassfish.jersey.containers</groupId>
8581
<artifactId>jersey-container-servlet</artifactId>
@@ -90,15 +86,21 @@
9086
<artifactId>jersey-media-json-jackson</artifactId>
9187
<version>${jersey.version}</version>
9288
</dependency>
89+
<dependency>
90+
<groupId>org.glassfish.jersey.core</groupId>
91+
<artifactId>jersey-client</artifactId>
92+
<version>${jersey.version}</version>
93+
</dependency>
94+
9395
<!-- servlet api -->
9496
<dependency>
9597
<groupId>javax.servlet</groupId>
9698
<artifactId>javax.servlet-api</artifactId>
9799
<version>${servlet-api-version}</version>
98100
<scope>provided</scope>
99101
</dependency>
100-
<!-- optional library -->
101102

103+
<!-- optional library -->
102104
<dependency>
103105
<groupId>org.glassfish.jersey.ext</groupId>
104106
<artifactId>jersey-spring3</artifactId>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.client.rest;
2+
3+
import javax.ws.rs.client.Client;
4+
import javax.ws.rs.client.ClientBuilder;
5+
import javax.ws.rs.client.Entity;
6+
import javax.ws.rs.core.MediaType;
7+
import javax.ws.rs.core.Response;
8+
9+
import com.baeldung.server.model.Employee;
10+
11+
public class RestClient {
12+
13+
private static final String REST_URI = "http://localhost:8082/spring-jersey/resources/employees";
14+
private Client client = ClientBuilder.newClient();
15+
16+
public Response createJsonEmployee(Employee emp) {
17+
return client.target(REST_URI).request(MediaType.APPLICATION_JSON).post(Entity.entity(emp, MediaType.APPLICATION_JSON));
18+
}
19+
20+
public Employee getJsonEmployee(int id) {
21+
return client.target(REST_URI).path(new Integer(id).toString()).request(MediaType.APPLICATION_JSON).get(Employee.class);
22+
}
23+
24+
public Response createXmlEmployee(Employee emp) {
25+
return client.target(REST_URI).request(MediaType.APPLICATION_XML).post(Entity.entity(emp, MediaType.APPLICATION_XML));
26+
}
27+
28+
public Employee getXmlEmployee(int id) {
29+
return client.target(REST_URI).path(new Integer(id).toString()).request(MediaType.APPLICATION_XML).get(Employee.class);
30+
}
31+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.client;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import javax.ws.rs.core.Response;
6+
7+
import org.junit.Test;
8+
9+
import com.baeldung.client.rest.RestClient;
10+
import com.baeldung.server.model.Employee;
11+
12+
public class JerseyClientLiveTest {
13+
14+
public static final int HTTP_CREATED = 201;
15+
16+
private RestClient client = new RestClient();
17+
18+
@Test
19+
public void givenCorrectObject_whenCorrectJsonRequest_thenResponseCodeCreated() {
20+
Employee emp = new Employee(6, "Johny");
21+
22+
Response response = client.createJsonEmployee(emp);
23+
24+
assertEquals(response.getStatus(), HTTP_CREATED);
25+
}
26+
27+
@Test
28+
public void givenCorrectObject_whenCorrectXmlRequest_thenResponseCodeCreated() {
29+
Employee emp = new Employee(7, "Jacky");
30+
31+
Response response = client.createXmlEmployee(emp);
32+
33+
assertEquals(response.getStatus(), HTTP_CREATED);
34+
}
35+
36+
@Test
37+
public void givenCorrectId_whenCorrectJsonRequest_thenCorrectEmployeeRetrieved() {
38+
int employeeId = 1;
39+
40+
Employee emp = client.getJsonEmployee(employeeId);
41+
42+
assertEquals(emp.getFirstName(), "Jane");
43+
}
44+
45+
@Test
46+
public void givenCorrectId_whenCorrectXmlRequest_thenCorrectEmployeeRetrieved() {
47+
int employeeId = 1;
48+
49+
Employee emp = client.getXmlEmployee(employeeId);
50+
51+
assertEquals(emp.getFirstName(), "Jane");
52+
}
53+
}

‎spring-jersey/src/test/java/com/baeldung/server/JerseyApiLiveTest.java‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.baeldung.server;
22

3+
4+
import static org.junit.Assert.assertEquals;
5+
36
import com.baeldung.server.model.Employee;
47
import com.fasterxml.jackson.databind.ObjectMapper;
58
import org.apache.http.HttpResponse;
@@ -15,15 +18,15 @@
1518

1619
public class JerseyApiLiveTest {
1720

18-
private static final String SERVICE_URL = "http://localhost:8082/jersey-api/resources/employees";
21+
private static final String SERVICE_URL = "http://localhost:8082/spring-jersey/resources/employees";
1922

2023
@Test
2124
public void givenGetAllEmployees_whenCorrectRequest_thenResponseCodeSuccess() throws IOException {
2225
final HttpUriRequest request = new HttpGet(SERVICE_URL);
2326

2427
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
2528

26-
assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK);
29+
assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_OK);
2730
}
2831

2932
@Test
@@ -32,7 +35,7 @@ public void givenGetEmployee_whenEmployeeExists_thenResponseCodeSuccess() throws
3235

3336
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
3437

35-
assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK);
38+
assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_OK);
3639
}
3740

3841
@Test
@@ -41,7 +44,7 @@ public void givenGetEmployee_whenEmployeeDoesNotExist_thenResponseCodeNotFound()
4144

4245
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
4346

44-
assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND);
47+
assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_NOT_FOUND);
4548
}
4649

4750
@Test
@@ -53,7 +56,7 @@ public void givenGetEmployee_whenJsonRequested_thenCorrectDataRetrieved() throws
5356
ObjectMapper mapper = new ObjectMapper();
5457
Employee emp = mapper.readValue(httpResponse.getEntity().getContent(), Employee.class);
5558

56-
assert(emp.getFirstName().equals("Jane"));
59+
assertEquals(emp.getFirstName(), "Jane");
5760
}
5861

5962
@Test
@@ -68,7 +71,7 @@ public void givenAddEmployee_whenJsonRequestSent_thenResponseCodeCreated() throw
6871
request.setEntity(input);
6972
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
7073

71-
assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED);
74+
assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_CREATED);
7275
}
7376

7477
@Test
@@ -83,7 +86,7 @@ public void givenAddEmployee_whenRequestForExistingObjectSent_thenResponseCodeCo
8386
request.setEntity(input);
8487
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
8588

86-
assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_CONFLICT);
89+
assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_CONFLICT);
8790
}
8891

8992
}

0 commit comments

Comments
 (0)