Skip to content

Commit 1fa404a

Browse files
committed
Resteasy Proxy client
1 parent cc06e2c commit 1fa404a

File tree

8 files changed

+144
-36
lines changed

8 files changed

+144
-36
lines changed

Java-RESTful-Client-Example/.classpath

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
<attribute name="maven.pomderived" value="true"/>
77
</attributes>
88
</classpathentry>
9-
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10-
<attributes>
11-
<attribute name="optional" value="true"/>
12-
<attribute name="maven.pomderived" value="true"/>
13-
</attributes>
14-
</classpathentry>
159
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
1610
<attributes>
1711
<attribute name="maven.pomderived" value="true"/>
@@ -27,10 +21,5 @@
2721
<attribute name="maven.pomderived" value="true"/>
2822
</attributes>
2923
</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>
3524
<classpathentry kind="output" path="target/classes"/>
3625
</classpath>

Java-RESTful-Client-Example/src/main/java/com/howtoprogram/service/BookRepository.java renamed to Java-RESTful-Client-Example/src/main/java/com/howtoprogram/repository/BookRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.howtoprogram.service;
1+
package com.howtoprogram.repository;
22

33
import java.util.List;
44

Java-RESTful-Client-Example/src/main/java/com/howtoprogram/service/BookRepositoryImplApacheHttpClient.java renamed to Java-RESTful-Client-Example/src/main/java/com/howtoprogram/repository/BookRepositoryImplApacheHttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.howtoprogram.service;
1+
package com.howtoprogram.repository;
22

33
import java.util.concurrent.Future;
44

Java-RESTful-Client-Example/src/main/java/com/howtoprogram/service/BookRepositoryImplJersey.java renamed to Java-RESTful-Client-Example/src/main/java/com/howtoprogram/repository/BookRepositoryImplJersey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.howtoprogram.service;
1+
package com.howtoprogram.repository;
22

33
import javax.ws.rs.client.Client;
44
import javax.ws.rs.client.ClientBuilder;

Java-RESTful-Client-Example/src/main/java/com/howtoprogram/service/BookRepositoryImplResteasy.java renamed to Java-RESTful-Client-Example/src/main/java/com/howtoprogram/repository/BookRepositoryImplResteasy.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
package com.howtoprogram.service;
2-
3-
1+
package com.howtoprogram.repository;
42

3+
import javax.ws.rs.client.Client;
4+
import javax.ws.rs.client.ClientBuilder;
55
import javax.ws.rs.client.Entity;
6+
import javax.ws.rs.client.WebTarget;
67
import javax.ws.rs.core.MediaType;
78
import javax.ws.rs.core.Response;
89

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-
1310
import com.howtoprogram.domain.Book;
1411

1512
public class BookRepositoryImplResteasy {
1613
private static final String URI_BOOK = "http://localhost:8080/v1/books";
1714

1815
public void deleteBook(Long id) {
19-
ResteasyClient client = new ResteasyClientBuilder().build();
20-
ResteasyWebTarget target = client.target(URI_BOOK).path(String.valueOf(id));
16+
Client client = ClientBuilder.newClient();
17+
WebTarget target = client.target(URI_BOOK).path(String.valueOf(id));
2118
Response response = target.request().delete();
2219
System.out.println("Status code:" + response.getStatus());
2320
}
2421

22+
public static void main(String[] args) throws Exception {
23+
BookRepositoryImplResteasy bookRepository = new BookRepositoryImplResteasy();
24+
// Getting the first book from the RESTful service
25+
Book book = bookRepository.getAllBooks()[0];
26+
bookRepository.deleteBook(book.getId());
27+
28+
}
2529

2630
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()));
31+
Client client = ClientBuilder.newClient();
32+
WebTarget target = client.target(URI_BOOK).path(String.valueOf(book.getId()));
2933
Response response = target.request(MediaType.APPLICATION_JSON_TYPE)
3034
.put(Entity.entity(book, MediaType.APPLICATION_JSON_TYPE));
3135
int status = response.getStatus();
@@ -34,16 +38,9 @@ public Book updateBook(Book book) throws Exception {
3438
return createdBook;
3539
}
3640

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-
4441

4542
public Book createBook(Book book) throws Exception {
46-
ResteasyClient client = new ResteasyClientBuilder().build();
43+
Client client = ClientBuilder.newClient();
4744
Response response = client.target(URI_BOOK).request()
4845
.post(Entity.entity(book, MediaType.APPLICATION_JSON_TYPE));
4946
int status = response.getStatus();
@@ -56,7 +53,7 @@ public Book createBook(Book book) throws Exception {
5653

5754

5855
public Book[] getAllBooks() throws Exception {
59-
ResteasyClient client = new ResteasyClientBuilder().build();
56+
Client client = ClientBuilder.newClient();
6057
Response response = client.target(URI_BOOK).request().get();
6158
int status = response.getStatus();
6259
System.out.println("Status code: " + status);
@@ -65,7 +62,6 @@ public Book[] getAllBooks() throws Exception {
6562
}
6663

6764

68-
6965
public Book findBookById(Long id) {
7066
return null;
7167
}

Java-RESTful-Client-Example/src/main/java/com/howtoprogram/service/BookRepositoryImplSpring.java renamed to Java-RESTful-Client-Example/src/main/java/com/howtoprogram/repository/BookRepositoryImplSpring.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.howtoprogram.service;
1+
package com.howtoprogram.repository;
22

33
import org.springframework.http.HttpStatus;
44
import org.springframework.http.ResponseEntity;
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.howtoprogram.repository.resteasyproxy;
2+
3+
import java.util.List;
4+
5+
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
6+
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
7+
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
8+
9+
import com.howtoprogram.domain.Book;
10+
11+
public class BookRepositoryImplResteasyProxy {
12+
private static final String URI_BOOK = "http://localhost:8080/v1/books";
13+
14+
public void deleteBook(Long id) {
15+
ResteasyClient client = new ResteasyClientBuilder().build();
16+
ResteasyWebTarget target = client.target(URI_BOOK);
17+
SimpleResteasyProxyClient simpleClient = target.proxy(SimpleResteasyProxyClient.class);
18+
simpleClient.deleteBook(id);
19+
}
20+
21+
public static void main(String[] args) throws Exception {
22+
BookRepositoryImplResteasyProxy bookRepository = new BookRepositoryImplResteasyProxy();
23+
Book book = bookRepository.getAllBooks().get(0);
24+
bookRepository.deleteBook(book.getId());
25+
}
26+
27+
/*
28+
* public static void main(String[] args) throws Exception { BookRepositoryImplResteasyProxy
29+
* bookRepository = new BookRepositoryImplResteasyProxy(); // Getting the first book from the
30+
* RESTful service Book book = bookRepository.getAllBooks().get(0); System.out.println(book); //
31+
* Change the name book.setName(book.getName() + " 3rd"); // Then update the book book =
32+
* bookRepository.updateBook(book); System.out.println(book); }
33+
*/
34+
35+
36+
/*
37+
* public static void main(String[] args) throws Exception { BookRepositoryImplResteasyProxy
38+
* bookRepository = new BookRepositoryImplResteasyProxy(); Book book = new Book(null,
39+
* "Effective Java", "Joshua Bloch"); Book createdBook = bookRepository.createBook(book);
40+
* System.out.println(createdBook); }
41+
*/
42+
43+
44+
/*
45+
* public static void main(String[] args) throws Exception { BookRepositoryImplResteasyProxy
46+
* bookRepository = new BookRepositoryImplResteasyProxy(); List<Book> books =
47+
* bookRepository.getAllBooks(); System.out.println(books); }
48+
*/
49+
public Book updateBook(Book book) throws Exception {
50+
51+
ResteasyClient client = new ResteasyClientBuilder().build();
52+
ResteasyWebTarget target = client.target(URI_BOOK);
53+
SimpleResteasyProxyClient simpleClient = target.proxy(SimpleResteasyProxyClient.class);
54+
Book updatedBook = simpleClient.updateBook(book.getId(), book);
55+
return updatedBook;
56+
}
57+
58+
59+
public Book createBook(Book book) throws Exception {
60+
61+
ResteasyClient client = new ResteasyClientBuilder().build();
62+
ResteasyWebTarget target = client.target(URI_BOOK);
63+
SimpleResteasyProxyClient simpleClient = target.proxy(SimpleResteasyProxyClient.class);
64+
Book createdBook = simpleClient.createBook(book);
65+
return createdBook;
66+
67+
}
68+
69+
70+
71+
public List<Book> getAllBooks() throws Exception {
72+
73+
ResteasyClient client = new ResteasyClientBuilder().build();
74+
ResteasyWebTarget target = client.target(URI_BOOK);
75+
SimpleResteasyProxyClient simpleClient = target.proxy(SimpleResteasyProxyClient.class);
76+
return simpleClient.getAllBooks();
77+
78+
}
79+
80+
81+
public Book findBookById(Long id) {
82+
return null;
83+
}
84+
85+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.howtoprogram.repository.resteasyproxy;
2+
3+
import java.util.List;
4+
5+
import javax.ws.rs.Consumes;
6+
import javax.ws.rs.DELETE;
7+
import javax.ws.rs.GET;
8+
import javax.ws.rs.POST;
9+
import javax.ws.rs.PUT;
10+
import javax.ws.rs.Path;
11+
import javax.ws.rs.PathParam;
12+
import javax.ws.rs.Produces;
13+
14+
import com.howtoprogram.domain.Book;
15+
16+
public interface SimpleResteasyProxyClient {
17+
18+
@GET
19+
@Consumes("application/json")
20+
List<Book> getAllBooks();
21+
22+
@POST
23+
@Consumes("application/json")
24+
@Produces("application/json")
25+
Book createBook(Book book);
26+
27+
@PUT
28+
@Path("/{id}")
29+
@Consumes("application/json")
30+
@Produces("application/json")
31+
Book updateBook(@PathParam("id") Long id, Book book);
32+
33+
@DELETE
34+
@Path("/{id}")
35+
@Consumes
36+
void deleteBook(@PathParam("id") Long id);
37+
38+
}

0 commit comments

Comments
 (0)