Skip to content

Commit cc06e2c

Browse files
committed
RESTful client with Resteasy Client API
1 parent 04fe744 commit cc06e2c

File tree

9 files changed

+172
-71
lines changed

9 files changed

+172
-71
lines changed

Book-RESTful-Service/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ This RESTful ws provides API for all below sub projects:
2020
###[Simple Java RESTful Web Service Clients](http://howtoprogram.xyz/2016/07/02/java-restful-web-service-clients/)
2121
###[RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
2222
###[Java RESTful Client With Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-restful-client-spring-apache-httpcomponents/)
23-
###[Java RESTful Client With Jersey Client](http://howtoprogram.xyz/2016/07/05/java-restful-client-jersey-client/)
23+
###[Java RESTful Client With Jersey Client](http://howtoprogram.xyz/2016/07/05/java-restful-client-jersey-client/)
24+
###[Java RESTful Client With Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-restful-client-resteasy-client/)

Java-JsonPojo-Example/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ Source code are described in **[Convert Java Objects To JSON And Vice Versa](htt
2020
###[Simple Java RESTful Web Service Clients](http://howtoprogram.xyz/2016/07/02/java-restful-web-service-clients/)
2121
###[RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
2222
###[Java RESTful Client With Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-restful-client-spring-apache-httpcomponents/)
23-
###[Java RESTful Client With Jersey Client](http://howtoprogram.xyz/2016/07/05/java-restful-client-jersey-client/)
23+
###[Java RESTful Client With Jersey Client](http://howtoprogram.xyz/2016/07/05/java-restful-client-jersey-client/)
24+
###[Java RESTful Client With Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-restful-client-resteasy-client/)

Java-RESTful-Client-Example/.classpath

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,15 @@
2222
<attribute name="maven.pomderived" value="true"/>
2323
</attributes>
2424
</classpathentry>
25+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
31+
<attributes>
32+
<attribute name="maven.pomderived" value="true"/>
33+
</attributes>
34+
</classpathentry>
2535
<classpathentry kind="output" path="target/classes"/>
2636
</classpath>

Java-RESTful-Client-Example/.project

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
</arguments>
1212
</buildCommand>
1313
<buildCommand>
14-
<name>org.eclipse.m2e.core.maven2Builder</name>
14+
<name>org.springframework.ide.eclipse.core.springbuilder</name>
1515
<arguments>
1616
</arguments>
1717
</buildCommand>
1818
<buildCommand>
19-
<name>org.springframework.ide.eclipse.core.springbuilder</name>
19+
<name>org.eclipse.m2e.core.maven2Builder</name>
2020
<arguments>
2121
</arguments>
2222
</buildCommand>

Java-RESTful-Client-Example/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ The **BookRepositoryImplJackson.java** includes all raw examples which we use Ja
1616
The **BookRepositoryImplSpring.java** includes all raw examples about creating RESTful Client With Spring RestTemplate.
1717
The **BookRepositoryImplApacheHttpClient.java** includes all raw examples about creating RESTful Client With Apache Httpcomponents
1818
The **BookRepositoryImplJersey.java** includes all raw examples about creating RESTful Client With Jersey Client library.
19+
The **BookRepositoryImplResteasy.java** includes all raw examples about creating RESTful Client With Resteasy Client API.
1920

2021
Source code are described:
2122
###[Simple Java RESTful Web Service Clients](http://howtoprogram.xyz/2016/07/02/java-restful-web-service-clients/)
2223
###[Java RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
23-
## 3. Related articles
2424
###[Convert Java Objects To JSON And Vice Versa](http://howtoprogram.xyz/2016/07/01/convert-java-objects-json-vice-versa/)
25-
###[RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
2625
###[Java RESTful Client With Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-restful-client-spring-apache-httpcomponents/)
27-
###[Java RESTful Client With Jersey Client](http://howtoprogram.xyz/2016/07/05/java-restful-client-jersey-client/)
26+
###[Java RESTful Client With Jersey Client](http://howtoprogram.xyz/2016/07/05/java-restful-client-jersey-client/)
27+
###[Java RESTful Client With Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-restful-client-resteasy-client/)
Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,72 @@
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-
<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>
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+
<!-- Begin Jersey dependencies -->
46+
<dependency>
47+
<groupId>org.glassfish.jersey.core</groupId>
48+
<artifactId>jersey-client</artifactId>
49+
<version>2.23.1</version>
50+
<scope>provided</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.glassfish.jersey.media</groupId>
54+
<artifactId>jersey-media-json-jackson</artifactId>
55+
<version>2.23.1</version>
56+
</dependency>
57+
<!-- End Jersey dependencies -->
58+
59+
<!-- Begin Resteasy dependencies -->
60+
<dependency>
61+
<groupId>org.jboss.resteasy</groupId>
62+
<artifactId>resteasy-client</artifactId>
63+
<version>3.0.17.Final</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.jboss.resteasy</groupId>
67+
<artifactId>resteasy-jackson2-provider</artifactId>
68+
<version>3.0.17.Final</version>
69+
</dependency>
70+
<!-- End Resteasy dependencies -->
71+
</dependencies>
5772
</project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.howtoprogram.service;
2+
3+
4+
5+
import javax.ws.rs.client.Entity;
6+
import javax.ws.rs.core.MediaType;
7+
import javax.ws.rs.core.Response;
8+
9+
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
10+
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
11+
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
12+
13+
import com.howtoprogram.domain.Book;
14+
15+
public class BookRepositoryImplResteasy {
16+
private static final String URI_BOOK = "http://localhost:8080/v1/books";
17+
18+
public void deleteBook(Long id) {
19+
ResteasyClient client = new ResteasyClientBuilder().build();
20+
ResteasyWebTarget target = client.target(URI_BOOK).path(String.valueOf(id));
21+
Response response = target.request().delete();
22+
System.out.println("Status code:" + response.getStatus());
23+
}
24+
25+
26+
public Book updateBook(Book book) throws Exception {
27+
ResteasyClient client = new ResteasyClientBuilder().build();
28+
ResteasyWebTarget target = client.target(URI_BOOK).path(String.valueOf(book.getId()));
29+
Response response = target.request(MediaType.APPLICATION_JSON_TYPE)
30+
.put(Entity.entity(book, MediaType.APPLICATION_JSON_TYPE));
31+
int status = response.getStatus();
32+
System.out.println("Status code: " + status);
33+
Book createdBook = response.readEntity(Book.class);
34+
return createdBook;
35+
}
36+
37+
public static void main(String[] args) throws Exception {
38+
BookRepositoryImplResteasy bookRepository = new BookRepositoryImplResteasy();
39+
Book book = new Book(null, "Effective Java", "Joshua Bloch");
40+
Book createdBook = bookRepository.createBook(book);
41+
System.out.println(createdBook);
42+
}
43+
44+
45+
public Book createBook(Book book) throws Exception {
46+
ResteasyClient client = new ResteasyClientBuilder().build();
47+
Response response = client.target(URI_BOOK).request()
48+
.post(Entity.entity(book, MediaType.APPLICATION_JSON_TYPE));
49+
int status = response.getStatus();
50+
System.out.println("Status code: " + status);
51+
Book createdBook = response.readEntity(Book.class);
52+
return createdBook;
53+
54+
}
55+
56+
57+
58+
public Book[] getAllBooks() throws Exception {
59+
ResteasyClient client = new ResteasyClientBuilder().build();
60+
Response response = client.target(URI_BOOK).request().get();
61+
int status = response.getStatus();
62+
System.out.println("Status code: " + status);
63+
Book[] books = response.readEntity(Book[].class);
64+
return books;
65+
}
66+
67+
68+
69+
public Book findBookById(Long id) {
70+
return null;
71+
}
72+
73+
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<configuration>
2-
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3-
<encoder>
4-
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
5-
</pattern>
6-
</encoder>
7-
</appender>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
5+
</pattern>
6+
</encoder>
7+
</appender>
88

9-
<logger name="deng" level="DEBUG" />
9+
<logger name="deng" level="INFO" />
1010

11-
<root level="DEBUG">
12-
<appender-ref ref="STDOUT" />
13-
</root>
11+
<root level="INFO">
12+
<appender-ref ref="STDOUT" />
13+
</root>
1414
</configuration>

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ This RESTful ws provides API for all below sub projects
1010
###[Simple Java RESTful Web Service Clients using *java.net.URL* package](http://howtoprogram.xyz/2016/07/02/java-restful-web-service-clients/)
1111
###[RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
1212
###[Java RESTful Client With Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-restful-client-spring-apache-httpcomponents/)
13-
###[Java RESTful Client With Jersey Client](http://howtoprogram.xyz/2016/07/05/java-restful-client-jersey-client/)
13+
###[Java RESTful Client With Jersey Client](http://howtoprogram.xyz/2016/07/05/java-restful-client-jersey-client/)
14+
###[Java RESTful Client With Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-restful-client-resteasy-client/)

0 commit comments

Comments
 (0)