Skip to content

Commit 948ae16

Browse files
committed
Java RESTful Client Using Apache CXF Proxy-based API
1 parent b3b9e3e commit 948ae16

File tree

8 files changed

+117
-6
lines changed

8 files changed

+117
-6
lines changed

Book-RESTful-Service/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ This RESTful ws provides API for all below sub projects:
2222
###[Java RESTful Client With Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-restful-client-spring-apache-httpcomponents/)
2323
###[Java RESTful Client With Jersey Client](http://howtoprogram.xyz/2016/07/05/java-restful-client-jersey-client/)
2424
###[Java RESTful Client With Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-restful-client-resteasy-client/)
25-
###[Java RESTful Client With Resteasy Client Proxy Framework](http://howtoprogram.xyz/2016/07/13/java-restful-client-resteasy-proxy-framework/)
25+
###[Java RESTful Client With Resteasy Client Proxy Framework](http://howtoprogram.xyz/2016/07/13/java-restful-client-resteasy-proxy-framework/)
26+
###[Java RESTful Client Using Apache CXF Proxy-based API](howtoprogram.xyz/2016/07/15/java-restful-client-using-apache-cxf-proxy-based-api/)

Java-JsonPojo-Example/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ Source code are described in **[Convert Java Objects To JSON And Vice Versa](htt
2222
###[Java RESTful Client With Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-restful-client-spring-apache-httpcomponents/)
2323
###[Java RESTful Client With Jersey Client](http://howtoprogram.xyz/2016/07/05/java-restful-client-jersey-client/)
2424
###[Java RESTful Client With Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-restful-client-resteasy-client/)
25-
###[Java RESTful Client With Resteasy Client Proxy Framework](http://howtoprogram.xyz/2016/07/13/java-restful-client-resteasy-proxy-framework/)
25+
###[Java RESTful Client With Resteasy Client Proxy Framework](http://howtoprogram.xyz/2016/07/13/java-restful-client-resteasy-proxy-framework/)
26+
###[Java RESTful Client Using Apache CXF Proxy-based API](howtoprogram.xyz/2016/07/15/java-restful-client-using-apache-cxf-proxy-based-api/)

Java-RESTful-Client-Example/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ The **BookRepositoryImplSpring.java** includes all raw examples about creating R
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.
1919
The **BookRepositoryImplResteasy.java** includes all raw examples about creating RESTful Client With Resteasy Client API.
20-
The **BookRepositoryImplResteasyProxy.java, SimpleResteasyProxyClient.java** includes all raw examples about creating RESTful Client With Resteasy Client Proxy Framework.
20+
The **BookRepositoryImplResteasyProxy.java, SimpleResteasyProxyClient.java** includes all raw examples about creating RESTful Client With Resteasy Client Proxy The **BookRepositoryImplCXFProxy.java, BookResource.java** includes all raw examples about creating Java RESTful Client Using Apache CXF Proxy-based API
21+
Framework.
2122

2223
Source code are described:
2324
###[Simple Java RESTful Web Service Clients](http://howtoprogram.xyz/2016/07/02/java-restful-web-service-clients/)
@@ -26,4 +27,4 @@ Source code are described:
2627
###[Java RESTful Client With Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-restful-client-spring-apache-httpcomponents/)
2728
###[Java RESTful Client With Jersey Client](http://howtoprogram.xyz/2016/07/05/java-restful-client-jersey-client/)
2829
###[Java RESTful Client With Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-restful-client-resteasy-client/)
29-
###[Java RESTful Client With Resteasy Client Proxy Framework](http://howtoprogram.xyz/2016/07/13/java-restful-client-resteasy-proxy-framework/)
30+
###[Java RESTful Client Using Apache CXF Proxy-based API](howtoprogram.xyz/2016/07/15/java-restful-client-using-apache-cxf-proxy-based-api/)

Java-RESTful-Client-Example/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,18 @@
6868
<version>3.0.17.Final</version>
6969
</dependency>
7070
<!-- End Resteasy dependencies -->
71+
<!-- Begin Apache CXF dependencies -->
72+
<dependency>
73+
<groupId>org.apache.cxf</groupId>
74+
<artifactId>cxf-rt-rs-client</artifactId>
75+
<version>3.0.3</version>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.apache.cxf</groupId>
79+
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
80+
<version>3.0.3</version>
81+
</dependency>
82+
83+
<!-- End Apache CXF dependencies -->
7184
</dependencies>
7285
</project>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.howtoprogram.repository.cxf;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
import javax.ws.rs.core.MediaType;
7+
8+
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
9+
import org.apache.cxf.jaxrs.client.WebClient;
10+
11+
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
12+
import com.howtoprogram.domain.Book;
13+
14+
public class BookRepositoryImplCXFProxy {
15+
private static final String URI_BOOK = "http://localhost:8080/v1/books";
16+
17+
public Book createBook(Book book) throws Exception {
18+
BookResource proxy = JAXRSClientFactory.create(URI_BOOK, BookResource.class,
19+
Collections.singletonList(new JacksonJsonProvider()));
20+
WebClient.client(proxy).type(MediaType.APPLICATION_JSON_TYPE);
21+
WebClient.client(proxy).accept(MediaType.APPLICATION_JSON_TYPE);
22+
Book createdBook = proxy.createBook(book);
23+
return createdBook;
24+
25+
}
26+
27+
public void deleteBook(Long id) {
28+
BookResource proxy = JAXRSClientFactory.create(URI_BOOK, BookResource.class);
29+
WebClient.client(proxy).accept(MediaType.APPLICATION_JSON_TYPE);
30+
proxy.deleteBook(id);
31+
}
32+
33+
public Book updateBook(Book book) throws Exception {
34+
BookResource proxy = JAXRSClientFactory.create(URI_BOOK, BookResource.class,
35+
Collections.singletonList(new JacksonJsonProvider()));
36+
WebClient.client(proxy).accept(MediaType.APPLICATION_JSON_TYPE);
37+
WebClient.client(proxy).type(MediaType.APPLICATION_JSON_TYPE);
38+
Book updatedBook = proxy.updateBook(book.getId(), book);
39+
return updatedBook;
40+
}
41+
42+
public List<Book> getAllBooks() throws Exception {
43+
BookResource proxy = JAXRSClientFactory.create(URI_BOOK, BookResource.class,
44+
Collections.singletonList(new JacksonJsonProvider()));
45+
WebClient.client(proxy).accept(MediaType.APPLICATION_JSON_TYPE);
46+
return proxy.getAllBooks();
47+
48+
}
49+
50+
51+
public static void main(String[] args) throws Exception {
52+
BookRepositoryImplCXFProxy bookRepository = new BookRepositoryImplCXFProxy();
53+
Book book = new Book(null, "Effective Java", "Joshua Bloch");
54+
Book createdBook = bookRepository.createBook(book);
55+
System.out.println(createdBook);
56+
}
57+
58+
59+
60+
public Book findBookById(Long id) {
61+
return null;
62+
}
63+
64+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.howtoprogram.repository.cxf;
2+
3+
import java.util.List;
4+
5+
import javax.ws.rs.DELETE;
6+
import javax.ws.rs.GET;
7+
import javax.ws.rs.POST;
8+
import javax.ws.rs.PUT;
9+
import javax.ws.rs.Path;
10+
import javax.ws.rs.PathParam;
11+
12+
import com.howtoprogram.domain.Book;
13+
14+
public interface BookResource {
15+
16+
@GET
17+
List<Book> getAllBooks();
18+
19+
@POST
20+
Book createBook(Book book);
21+
22+
@PUT
23+
@Path("/{id}")
24+
Book updateBook(@PathParam("id") Long id, Book book);
25+
26+
@DELETE
27+
@Path("/{id}")
28+
void deleteBook(@PathParam("id") Long id);
29+
30+
}

Java-RESTful-Client-Example/src/main/java/com/howtoprogram/repository/resteasyproxy/SimpleResteasyProxyClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface SimpleResteasyProxyClient {
3232

3333
@DELETE
3434
@Path("/{id}")
35-
@Consumes
35+
@Consumes("application/json")
3636
void deleteBook(@PathParam("id") Long id);
3737

3838
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ This RESTful ws provides API for all below sub projects
1212
###[Java RESTful Client With Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-restful-client-spring-apache-httpcomponents/)
1313
###[Java RESTful Client With Jersey Client](http://howtoprogram.xyz/2016/07/05/java-restful-client-jersey-client/)
1414
###[Java RESTful Client With Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-restful-client-resteasy-client/)
15-
###[Java RESTful Client With Resteasy Client Proxy Framework](http://howtoprogram.xyz/2016/07/13/java-restful-client-resteasy-proxy-framework/)
15+
###[Java RESTful Client With Resteasy Client Proxy Framework](http://howtoprogram.xyz/2016/07/13/java-restful-client-resteasy-proxy-framework/)
16+
###[Java RESTful Client Using Apache CXF Proxy-based API](howtoprogram.xyz/2016/07/15/java-restful-client-using-apache-cxf-proxy-based-api/)

0 commit comments

Comments
 (0)