Skip to content

Commit 04450ad

Browse files
committed
added Jersey client
1 parent cca34b0 commit 04450ad

File tree

4 files changed

+138
-42
lines changed

4 files changed

+138
-42
lines changed

Book-RESTful-Service/.project

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
<projects>
66
</projects>
77
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
813
<buildCommand>
914
<name>org.eclipse.jdt.core.javabuilder</name>
1015
<arguments>
@@ -15,9 +20,16 @@
1520
<arguments>
1621
</arguments>
1722
</buildCommand>
23+
<buildCommand>
24+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
1828
</buildSpec>
1929
<natures>
30+
<nature>org.springframework.ide.eclipse.core.springnature</nature>
2031
<nature>org.eclipse.jdt.core.javanature</nature>
2132
<nature>org.eclipse.m2e.core.maven2Nature</nature>
33+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
2234
</natures>
2335
</projectDescription>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<faceted-project>
3+
<installed facet="cloudfoundry.standalone.app" version="1.0"/>
4+
</faceted-project>
Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,57 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.howtoprogram</groupId>
5-
<artifactId>java-restful-client-example</artifactId>
6-
<version>0.0.1-SNAPSHOT</version>
7-
<properties>
8-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9-
<maven.compiler.source>1.8</maven.compiler.source>
10-
<maven.compiler.target>1.8</maven.compiler.target>
11-
</properties>
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.howtoprogram</groupId>
5+
<artifactId>java-restful-client-example</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<properties>
8+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9+
<maven.compiler.source>1.8</maven.compiler.source>
10+
<maven.compiler.target>1.8</maven.compiler.target>
11+
</properties>
1212

13-
<dependencies>
14-
<dependency>
15-
<groupId>com.fasterxml.jackson.core</groupId>
16-
<artifactId>jackson-databind</artifactId>
17-
<version>2.7.5</version>
18-
</dependency>
19-
<dependency>
20-
<groupId>com.fasterxml.jackson.dataformat</groupId>
21-
<artifactId>jackson-dataformat-xml</artifactId>
22-
<version>2.6.3</version>
23-
</dependency>
24-
<dependency>
25-
<groupId>org.springframework</groupId>
26-
<artifactId>spring-web</artifactId>
27-
<version>4.2.6.RELEASE</version>
28-
</dependency>
13+
<dependencies>
14+
<dependency>
15+
<groupId>com.fasterxml.jackson.core</groupId>
16+
<artifactId>jackson-databind</artifactId>
17+
<version>2.7.5</version>
18+
</dependency>
19+
<dependency>
20+
<groupId>com.fasterxml.jackson.dataformat</groupId>
21+
<artifactId>jackson-dataformat-xml</artifactId>
22+
<version>2.6.3</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework</groupId>
26+
<artifactId>spring-web</artifactId>
27+
<version>4.2.6.RELEASE</version>
28+
</dependency>
2929

30-
<dependency>
31-
<groupId>org.apache.httpcomponents</groupId>
32-
<artifactId>httpasyncclient</artifactId>
33-
<version>4.1.2</version>
34-
</dependency>
35-
<dependency>
36-
<groupId>org.slf4j</groupId>
37-
<artifactId>slf4j-api</artifactId>
38-
<version>1.7.21</version>
39-
</dependency>
40-
<dependency>
41-
<groupId>ch.qos.logback</groupId>
42-
<artifactId>logback-classic</artifactId>
43-
<version>1.1.7</version>
44-
</dependency>
45-
</dependencies>
30+
<dependency>
31+
<groupId>org.apache.httpcomponents</groupId>
32+
<artifactId>httpasyncclient</artifactId>
33+
<version>4.1.2</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.slf4j</groupId>
37+
<artifactId>slf4j-api</artifactId>
38+
<version>1.7.21</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>ch.qos.logback</groupId>
42+
<artifactId>logback-classic</artifactId>
43+
<version>1.1.7</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.glassfish.jersey.core</groupId>
47+
<artifactId>jersey-client</artifactId>
48+
<version>2.23.1</version>
49+
<scope>provided</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.glassfish.jersey.media</groupId>
53+
<artifactId>jersey-media-json-jackson</artifactId>
54+
<version>2.23.1</version>
55+
</dependency>
56+
</dependencies>
4657
</project>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.howtoprogram.service;
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.client.WebTarget;
7+
import javax.ws.rs.core.MediaType;
8+
import javax.ws.rs.core.Response;
9+
10+
import com.howtoprogram.domain.Book;
11+
12+
public class BookRepositoryImplJersey {
13+
private static final String URI_BOOK = "http://localhost:8080/v1/books";
14+
15+
public void deleteBook(Long id) {
16+
Client client = ClientBuilder.newClient();
17+
WebTarget target = client.target(URI_BOOK).path(String.valueOf(id));
18+
Response response = target.request().delete();
19+
System.out.println("Status code:" + response.getStatus());
20+
}
21+
22+
public static void main(String[] args) throws Exception {
23+
BookRepositoryImplJersey bookRepository = new BookRepositoryImplJersey();
24+
// Getting the first book from the RESTful service
25+
Book book = bookRepository.getAllBooks()[0];
26+
bookRepository.deleteBook(book.getId());
27+
28+
}
29+
30+
public Book updateBook(Book book) throws Exception {
31+
Client client = ClientBuilder.newClient();
32+
WebTarget target = client.target(URI_BOOK).path(String.valueOf(book.getId()));
33+
Response response = target.request(MediaType.APPLICATION_JSON_TYPE)
34+
.put(Entity.entity(book, MediaType.APPLICATION_JSON_TYPE));
35+
int status = response.getStatus();
36+
System.out.println("Status code: " + status);
37+
Book createdBook = response.readEntity(Book.class);
38+
return createdBook;
39+
}
40+
41+
42+
public Book createBook(Book book) throws Exception {
43+
Client client = ClientBuilder.newClient();
44+
Response response = client.target(URI_BOOK).request()
45+
.post(Entity.entity(book, MediaType.APPLICATION_JSON_TYPE));
46+
int status = response.getStatus();
47+
System.out.println("Status code: " + status);
48+
Book createdBook = response.readEntity(Book.class);
49+
return createdBook;
50+
51+
}
52+
53+
54+
55+
public Book[] getAllBooks() throws Exception {
56+
Client client = ClientBuilder.newClient();
57+
Response response = client.target(URI_BOOK).request().get();
58+
int status = response.getStatus();
59+
System.out.println("Status code: " + status);
60+
Book[] books = response.readEntity(Book[].class);
61+
return books;
62+
}
63+
64+
65+
public Book findBookById(Long id) {
66+
return null;
67+
}
68+
69+
}

0 commit comments

Comments
 (0)