1919
2020import com .google .gson .Gson ;
2121import com .google .gson .JsonIOException ;
22- import com .google .gson .stream .JsonReader ;
23- import dagger .Module ;
24- import dagger .Provides ;
2522import feign .Feign ;
2623import feign .Logger ;
2724import feign .RequestLine ;
3128import java .io .Reader ;
3229import java .lang .reflect .Type ;
3330import java .util .List ;
34- import javax .inject .Inject ;
3531import javax .inject .Named ;
36- import javax .inject .Singleton ;
3732
3833/** adapted from {@code com.example.retrofit.GitHubClient} */
3934public class GitHubExample {
@@ -48,8 +43,13 @@ static class Contributor {
4843 int contributions ;
4944 }
5045
51- public static void main (String ... args ) throws InterruptedException {
52- GitHub github = Feign .create (GitHub .class , "https://api.github.com" , new GitHubModule ());
46+ public static void main (String ... args ) {
47+ GitHub github =
48+ Feign .builder ()
49+ .logger (new Logger .ErrorLogger ())
50+ .logLevel (Logger .Level .BASIC )
51+ .decoder (new GsonDecoder ())
52+ .target (GitHub .class , "https://api.github.com" );
5353
5454 System .out .println ("Let's fetch and print a list of the contributors to this library." );
5555 List <Contributor > contributors = github .contributors ("netflix" , "feign" );
@@ -58,68 +58,25 @@ public static void main(String... args) throws InterruptedException {
5858 }
5959 }
6060
61- @ Module (overrides = true , library = true , includes = GsonModule .class )
62- static class GitHubModule {
63-
64- @ Provides
65- Logger .Level loggingLevel () {
66- return Logger .Level .BASIC ;
67- }
68-
69- @ Provides
70- Logger logger () {
71- return new Logger .ErrorLogger ();
72- }
73- }
74-
75- /**
76- * Here's how it looks to wire json codecs. Note, that you can always instead use {@code
77- * feign-gson}!
78- */
79- @ Module (library = true )
80- static class GsonModule {
81-
82- @ Provides
83- @ Singleton
84- Gson gson () {
85- return new Gson ();
86- }
87-
88- @ Provides
89- Decoder decoder (GsonDecoder gsonDecoder ) {
90- return gsonDecoder ;
91- }
92- }
93-
61+ /** Here's how it looks to write a decoder. Note: you can instead use {@code feign-gson}! */
9462 static class GsonDecoder implements Decoder {
95- private final Gson gson ;
96-
97- @ Inject
98- GsonDecoder (Gson gson ) {
99- this .gson = gson ;
100- }
63+ private final Gson gson = new Gson ();
10164
10265 @ Override
10366 public Object decode (Response response , Type type ) throws IOException {
104- if (response .body () == null ) {
67+ if (void . class == type || response .body () == null ) {
10568 return null ;
10669 }
10770 Reader reader = response .body ().asReader ();
10871 try {
109- return fromJson (new JsonReader (reader ), type );
110- } finally {
111- ensureClosed (reader );
112- }
113- }
114-
115- private Object fromJson (JsonReader jsonReader , Type type ) throws IOException {
116- try {
117- return gson .fromJson (jsonReader , type );
72+ return gson .fromJson (reader , type );
11873 } catch (JsonIOException e ) {
11974 if (e .getCause () != null && e .getCause () instanceof IOException ) {
12075 throw IOException .class .cast (e .getCause ());
12176 }
12277 throw e ;
78+ } finally {
79+ ensureClosed (reader );
12380 }
12481 }
12582 }
0 commit comments