1717
1818import com .google .gson .Gson ;
1919import com .google .gson .JsonIOException ;
20- import com .google .gson .stream .JsonReader ;
21- import dagger .Module ;
22- import dagger .Provides ;
2320import feign .Feign ;
2421import feign .Logger ;
2522import feign .RequestLine ;
2623import feign .Response ;
2724import feign .codec .Decoder ;
2825
29- import javax .inject .Inject ;
3026import javax .inject .Named ;
31- import javax .inject .Singleton ;
3227import java .io .IOException ;
3328import java .io .Reader ;
3429import java .lang .reflect .Type ;
@@ -51,8 +46,12 @@ static class Contributor {
5146 int contributions ;
5247 }
5348
54- public static void main (String ... args ) throws InterruptedException {
55- GitHub github = Feign .create (GitHub .class , "https://api.github.com" , new GitHubModule ());
49+ public static void main (String ... args ) {
50+ GitHub github = Feign .builder ()
51+ .logger (new Logger .ErrorLogger ())
52+ .logLevel (Logger .Level .BASIC )
53+ .decoder (new GsonDecoder ())
54+ .target (GitHub .class , "https://api.github.com" );
5655
5756 System .out .println ("Let's fetch and print a list of the contributors to this library." );
5857 List <Contributor > contributors = github .contributors ("netflix" , "feign" );
@@ -61,60 +60,26 @@ public static void main(String... args) throws InterruptedException {
6160 }
6261 }
6362
64- @ Module (overrides = true , library = true , includes = GsonModule .class )
65- static class GitHubModule {
66-
67- @ Provides Logger .Level loggingLevel () {
68- return Logger .Level .BASIC ;
69- }
70-
71- @ Provides Logger logger () {
72- return new Logger .ErrorLogger ();
73- }
74- }
75-
7663 /**
77- * Here's how it looks to wire json codecs . Note, that you can always instead use {@code feign-gson}!
64+ * Here's how it looks to write a decoder . Note: you can instead use {@code feign-gson}!
7865 */
79- @ Module (library = true )
80- static class GsonModule {
81-
82- @ Provides @ Singleton Gson gson () {
83- return new Gson ();
84- }
85-
86- @ Provides Decoder decoder (GsonDecoder gsonDecoder ) {
87- return gsonDecoder ;
88- }
89- }
90-
9166 static class GsonDecoder implements Decoder {
92- private final Gson gson ;
93-
94- @ Inject GsonDecoder (Gson gson ) {
95- this .gson = gson ;
96- }
67+ private final Gson gson = new Gson ();
9768
9869 @ Override public Object decode (Response response , Type type ) throws IOException {
99- if (response .body () == null ) {
70+ if (void . class == type || response .body () == null ) {
10071 return null ;
10172 }
10273 Reader reader = response .body ().asReader ();
10374 try {
104- return fromJson (new JsonReader (reader ), type );
105- } finally {
106- ensureClosed (reader );
107- }
108- }
109-
110- private Object fromJson (JsonReader jsonReader , Type type ) throws IOException {
111- try {
112- return gson .fromJson (jsonReader , type );
75+ return gson .fromJson (reader , type );
11376 } catch (JsonIOException e ) {
11477 if (e .getCause () != null && e .getCause () instanceof IOException ) {
11578 throw IOException .class .cast (e .getCause ());
11679 }
11780 throw e ;
81+ } finally {
82+ ensureClosed (reader );
11883 }
11984 }
12085 }
0 commit comments