Skip to content

Commit 7fb2017

Browse files
author
adriancole
committed
support usage of gson without using dagger
1 parent eb03e20 commit 7fb2017

7 files changed

Lines changed: 90 additions & 81 deletions

File tree

‎CHANGES.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### Version 5.2.0
2+
* Support usage of `GsonCodec` via `Feign.Builder`
3+
14
### Version 5.1.0
25
* Correctly handle IOExceptions wrapped by Ribbon.
36
* Miscellaneous findbugs fixes.

‎README.md‎

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ static class Contributor {
2525
}
2626

2727
public static void main(String... args) {
28-
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GsonModule());
28+
GitHub github = Feign.builder()
29+
.decoder(new GsonCodec())
30+
.target(GitHub.class, "https://api.github.com");
2931

3032
// Fetch and print a list of the contributors to this library.
3133
List<Contributor> contributors = github.contributors("netflix", "feign");
@@ -35,8 +37,6 @@ public static void main(String... args) {
3537
}
3638
```
3739

38-
Feign includes a fully functional json codec in the `feign-gson` extension. See the `Decoder` section for how to write your own.
39-
4040
### Customization
4141

4242
Feign has several aspects that can be customized. For simple cases, you can use `Feign.builder()` to construct an API interface with your custom components. For example:
@@ -83,9 +83,14 @@ Feign intends to work well within Netflix and other Open Source communities. Mo
8383
### Gson
8484
[GsonModule](https://github.com/Netflix/feign/tree/master/gson) adds default encoders and decoders so you get get started with a JSON api.
8585

86-
Integration requires you pass `new GsonModule()` to `Feign.create()`, or add it to your graph with Dagger:
86+
Add `GsonCodec` to your `Feign.Builder` like so:
87+
8788
```java
88-
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GsonModule());
89+
GsonCodec codec = new GsonCodec();
90+
GitHub github = Feign.builder()
91+
.encoder(codec)
92+
.decoder(codec)
93+
.target(GitHub.class, "https://api.github.com");
8994
```
9095

9196
### Sax
@@ -119,26 +124,16 @@ MyService api = Feign.create(MyService.class, "https://myAppProd", new RibbonMod
119124
```
120125

121126
### Decoders
122-
The last argument to `Feign.create` allows you to specify additional configuration such as how to decode a responses, modeled in Dagger.
127+
`Feign.builder()` allows you to specify additional configuration such as how to decode a response.
123128

124129
If any methods in your interface return types besides `Response` or `void`, you'll need to configure a `Decoder`.
125130

126-
The `GsonModule` in the `feign-gson` extension configures a `Decoder` which parses objects from JSON using reflection.
131+
Here's how to configure json decoding (using the `feign-gson` extension):
127132

128-
Here's how you could write this yourself, using whatever library you prefer:
129133
```java
130-
@Module(library = true)
131-
static class JsonModule {
132-
@Provides Decoder decoder(final JsonParser parser) {
133-
return new Decoder() {
134-
135-
@Override public Object decode(Response response, Type type) throws IOException {
136-
return parser.readJson(response.body().asReader(), type);
137-
}
138-
139-
};
140-
}
141-
}
134+
GitHub github = Feign.builder()
135+
.decoder(new GsonCodec())
136+
.target(GitHub.class, "https://api.github.com");
142137
```
143138

144139
### Advanced usage and Dagger
@@ -166,15 +161,9 @@ Where possible, Feign configuration uses normal Dagger conventions. For example
166161
#### Logging
167162
You can log the http messages going to and from the target by setting up a `Logger`. Here's the easiest way to do that:
168163
```java
169-
@Module(overrides = true)
170-
class Overrides {
171-
@Provides @Singleton Logger.Level provideLoggerLevel() {
172-
return Logger.Level.FULL;
173-
}
174-
175-
@Provides @Singleton Logger provideLogger() {
176-
return new Logger.JavaLogger().appendToFile("logs/http.log");
177-
}
178-
}
179-
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GsonGitHubModule(), new Overrides());
164+
GitHub github = Feign.builder()
165+
.decoder(new GsonCodec())
166+
.logger(new Logger.JavaLogger().appendToFile("logs/http.log"))
167+
.logLevel(Logger.Level.FULL)
168+
.target(GitHub.class, "https://api.github.com");
180169
```

‎gson/README.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ Gson Codec
33

44
This module adds support for encoding and decoding json via the Gson library.
55

6-
Add this to your object graph like so:
6+
Add `GsonCodec` to your `Feign.Builder` like so:
7+
8+
```java
9+
GsonCodec codec = new GsonCodec();
10+
GitHub github = Feign.builder()
11+
.encoder(codec)
12+
.decoder(codec)
13+
.target(GitHub.class, "https://api.github.com");
14+
```
15+
16+
Or.. to your object graph like so:
717

818
```java
919
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GsonModule());
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package feign.gson;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonIOException;
5+
import feign.RequestTemplate;
6+
import feign.Response;
7+
import feign.codec.Decoder;
8+
import feign.codec.Encoder;
9+
10+
import javax.inject.Inject;
11+
import java.io.IOException;
12+
import java.io.Reader;
13+
import java.lang.reflect.Type;
14+
15+
import static feign.Util.ensureClosed;
16+
17+
public class GsonCodec implements Encoder, Decoder {
18+
private final Gson gson;
19+
20+
public GsonCodec() {
21+
this(new Gson());
22+
}
23+
24+
@Inject public GsonCodec(Gson gson) {
25+
this.gson = gson;
26+
}
27+
28+
@Override public void encode(Object object, RequestTemplate template) {
29+
template.body(gson.toJson(object));
30+
}
31+
32+
@Override public Object decode(Response response, Type type) throws IOException {
33+
if (response.body() == null) {
34+
return null;
35+
}
36+
Reader reader = response.body().asReader();
37+
try {
38+
return gson.fromJson(reader, type);
39+
} catch (JsonIOException e) {
40+
if (e.getCause() != null && e.getCause() instanceof IOException) {
41+
throw IOException.class.cast(e.getCause());
42+
}
43+
throw e;
44+
} finally {
45+
ensureClosed(reader);
46+
}
47+
}
48+
}

‎gson/src/main/java/feign/gson/GsonModule.java‎

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.google.gson.Gson;
1919
import com.google.gson.GsonBuilder;
2020
import com.google.gson.InstanceCreator;
21-
import com.google.gson.JsonIOException;
2221
import com.google.gson.TypeAdapter;
2322
import com.google.gson.internal.ConstructorConstructor;
2423
import com.google.gson.internal.bind.MapTypeAdapterFactory;
@@ -27,21 +26,16 @@
2726
import com.google.gson.stream.JsonWriter;
2827
import dagger.Provides;
2928
import feign.Feign;
30-
import feign.RequestTemplate;
31-
import feign.Response;
3229
import feign.codec.Decoder;
3330
import feign.codec.Encoder;
3431

35-
import javax.inject.Inject;
3632
import javax.inject.Singleton;
3733
import java.io.IOException;
38-
import java.io.Reader;
3934
import java.lang.reflect.Type;
4035
import java.util.Collections;
4136
import java.util.Map;
4237
import java.util.Set;
4338

44-
import static feign.Util.ensureClosed;
4539
import static feign.Util.resolveLastTypeParameter;
4640

4741
/**
@@ -52,20 +46,20 @@
5246
* to read numbers in a {@code Map<String, Object>} as Integers. You can
5347
* customize further by adding additional set bindings to the raw type
5448
* {@code TypeAdapter}.
55-
*
49+
* <p/>
5650
* <br>
5751
* Here's an example of adding a custom json type adapter.
58-
*
52+
* <p/>
5953
* <pre>
6054
* &#064;Provides(type = Provides.Type.SET)
6155
* TypeAdapter upperZone() {
6256
* return new TypeAdapter&lt;Zone&gt;() {
63-
*
57+
*
6458
* &#064;Override
6559
* public void write(JsonWriter out, Zone value) throws IOException {
6660
* throw new IllegalArgumentException();
6761
* }
68-
*
62+
*
6963
* &#064;Override
7064
* public Zone read(JsonReader in) throws IOException {
7165
* in.beginObject();
@@ -91,41 +85,6 @@ public final class GsonModule {
9185
return codec;
9286
}
9387

94-
static class GsonCodec implements Encoder, Decoder {
95-
private final Gson gson;
96-
97-
@Inject GsonCodec(Gson gson) {
98-
this.gson = gson;
99-
}
100-
101-
@Override public void encode(Object object, RequestTemplate template) {
102-
template.body(gson.toJson(object));
103-
}
104-
105-
@Override public Object decode(Response response, Type type) throws IOException {
106-
if (response.body() == null) {
107-
return null;
108-
}
109-
Reader reader = response.body().asReader();
110-
try {
111-
return fromJson(new JsonReader(reader), type);
112-
} finally {
113-
ensureClosed(reader);
114-
}
115-
}
116-
117-
private Object fromJson(JsonReader jsonReader, Type type) throws IOException {
118-
try {
119-
return gson.fromJson(jsonReader, type);
120-
} catch (JsonIOException e) {
121-
if (e.getCause() != null && e.getCause() instanceof IOException) {
122-
throw IOException.class.cast(e.getCause());
123-
}
124-
throw e;
125-
}
126-
}
127-
}
128-
12988
@Provides @Singleton Gson gson(Set<TypeAdapter> adapters) {
13089
GsonBuilder builder = new GsonBuilder().setPrettyPrinting();
13190
for (TypeAdapter<?> adapter : adapters) {

‎gson/src/test/java/feign/gson/GsonModuleTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ static class EncoderAndDecoderBindings {
5252
EncoderAndDecoderBindings bindings = new EncoderAndDecoderBindings();
5353
ObjectGraph.create(bindings).inject(bindings);
5454

55-
assertEquals(bindings.encoder.getClass(), GsonModule.GsonCodec.class);
56-
assertEquals(bindings.decoder.getClass(), GsonModule.GsonCodec.class);
55+
assertEquals(bindings.encoder.getClass(), GsonCodec.class);
56+
assertEquals(bindings.decoder.getClass(), GsonCodec.class);
5757
}
5858

5959
@Module(includes = GsonModule.class, injects = EncoderBindings.class)

‎gson/src/test/java/feign/gson/examples/GitHubExample.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import feign.Feign;
1919
import feign.RequestLine;
20-
import feign.gson.GsonModule;
20+
import feign.gson.GsonCodec;
2121

2222
import javax.inject.Named;
2323
import java.util.List;
@@ -38,7 +38,7 @@ static class Contributor {
3838
}
3939

4040
public static void main(String... args) throws InterruptedException {
41-
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new GsonModule());
41+
GitHub github = Feign.builder().decoder(new GsonCodec()).target(GitHub.class, "https://api.github.com");
4242

4343
System.out.println("Let's fetch and print a list of the contributors to this library.");
4444
List<Contributor> contributors = github.contributors("netflix", "feign");

0 commit comments

Comments
 (0)