Skip to content

Commit cac35bd

Browse files
committed
Java RESTful Client Using Netflix Feign
1 parent 948ae16 commit cac35bd

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

Java-RESTful-Client-Example/README.md

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

2325
Source code are described:
2426
###[Simple Java RESTful Web Service Clients](http://howtoprogram.xyz/2016/07/02/java-restful-web-service-clients/)
@@ -27,4 +29,5 @@ Source code are described:
2729
###[Java RESTful Client With Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-restful-client-spring-apache-httpcomponents/)
2830
###[Java RESTful Client With Jersey Client](http://howtoprogram.xyz/2016/07/05/java-restful-client-jersey-client/)
2931
###[Java RESTful Client With Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-restful-client-resteasy-client/)
30-
###[Java RESTful Client Using Apache CXF Proxy-based API](howtoprogram.xyz/2016/07/15/java-restful-client-using-apache-cxf-proxy-based-api/)
32+
###[Java RESTful Client Using Apache CXF Proxy-based API](howtoprogram.xyz/2016/07/15/java-restful-client-using-apache-cxf-proxy-based-api/)
33+
###[Java RESTful Client Using Netflix Feign](http://howtoprogram.xyz/2016/07/18/java-restful-client-using-netflix-feign/)

Java-RESTful-Client-Example/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,17 @@
8181
</dependency>
8282

8383
<!-- End Apache CXF dependencies -->
84+
<!-- Begin Netflix Feign dependencies -->
85+
<dependency>
86+
<groupId>com.netflix.feign</groupId>
87+
<artifactId>feign-core</artifactId>
88+
<version>8.18.0</version>
89+
</dependency>
90+
<dependency>
91+
<groupId>com.netflix.feign</groupId>
92+
<artifactId>feign-jackson</artifactId>
93+
<version>8.18.0</version>
94+
</dependency>
95+
<!-- End Netflix Feign dependencies -->
8496
</dependencies>
8597
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.howtoprogram.repository.feign;
2+
3+
import java.util.List;
4+
5+
import com.howtoprogram.domain.Book;
6+
7+
import feign.Feign;
8+
import feign.jackson.JacksonDecoder;
9+
import feign.jackson.JacksonEncoder;
10+
11+
public class BookRepositoryImplFeign {
12+
private static final String URI_BOOK = "http://localhost:8080";
13+
14+
public Book updateBook(Book book) throws Exception {
15+
BookResourceFeign bookResource = Feign.builder().encoder(new JacksonEncoder())
16+
.decoder(new JacksonDecoder()).target(BookResourceFeign.class, URI_BOOK);
17+
Book updatedBook = bookResource.updateBook(book.getId(), book);
18+
return updatedBook;
19+
}
20+
21+
22+
public Book createBook(Book book) throws Exception {
23+
BookResourceFeign bookResource = Feign.builder().encoder(new JacksonEncoder())
24+
.decoder(new JacksonDecoder()).target(BookResourceFeign.class, URI_BOOK);
25+
Book createdBook = bookResource.createBook(book);
26+
return createdBook;
27+
28+
}
29+
30+
public List<Book> getAllBooks() throws Exception {
31+
BookResourceFeign bookResource = Feign.builder().encoder(new JacksonEncoder())
32+
.decoder(new JacksonDecoder()).target(BookResourceFeign.class, URI_BOOK);
33+
return bookResource.getAllBooks();
34+
35+
}
36+
37+
public static void main(String[] args) throws Exception {
38+
BookRepositoryImplFeign bookRepository = new BookRepositoryImplFeign();
39+
Book book = bookRepository.getAllBooks().get(0);
40+
bookRepository.deleteBook(book.getId());
41+
}
42+
43+
44+
45+
public void deleteBook(Long id) {
46+
BookResourceFeign bookResource = Feign.builder().encoder(new JacksonEncoder())
47+
.decoder(new JacksonDecoder()).target(BookResourceFeign.class, URI_BOOK);
48+
bookResource.deleteBook(id);
49+
}
50+
51+
52+
53+
public Book findBookById(Long id) {
54+
return null;
55+
}
56+
57+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.howtoprogram.repository.feign;
2+
3+
import java.util.List;
4+
5+
import com.howtoprogram.domain.Book;
6+
7+
import feign.Headers;
8+
import feign.Param;
9+
import feign.RequestLine;
10+
11+
@Headers("Accept: application/json")
12+
public interface BookResourceFeign {
13+
14+
@RequestLine("GET /v1/books")
15+
List<Book> getAllBooks();
16+
17+
@Headers("Content-Type: application/json")
18+
@RequestLine("POST /v1/books")
19+
Book createBook(Book book);
20+
21+
@Headers("Content-Type: application/json")
22+
@RequestLine("PUT /v1/books/{id}")
23+
Book updateBook(@Param("id") Long id, Book book);
24+
25+
@RequestLine("DELETE /v1/books/{id}")
26+
void deleteBook(@Param("id") Long id);
27+
28+
}

0 commit comments

Comments
 (0)