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 ;
55import javax .ws .rs .client .Entity ;
6+ import javax .ws .rs .client .WebTarget ;
67import javax .ws .rs .core .MediaType ;
78import 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-
1310import com .howtoprogram .domain .Book ;
1411
1512public 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 }
0 commit comments