Skip to content

Commit c92025c

Browse files
matthurneadrian
authored andcommitted
Squashed commit of the following:
commit 34eb575 Author: Matt Hurne <matt@thehurnes.com> Date: Tue Oct 15 15:54:20 2013 -0400 Remove unnecessary defensive close of Reader commit 38e5160 Author: Matt Hurne <matt@thehurnes.com> Date: Tue Oct 8 13:59:35 2013 -0400 Replace wildcard import with individual imports commit cc84581 Author: Matt Hurne <matt@thehurnes.com> Date: Tue Oct 8 13:55:37 2013 -0400 Revert GitHub example to use JacksonDecoder rather than JacksonModule now that JacksonDecoder behaves sensibly with its default ObjectMapper commit 8b96382 Author: Matt Hurne <matt@thehurnes.com> Date: Tue Oct 8 13:52:45 2013 -0400 Configure default ObjectMapper used by JacksonEncoder and JacksonDecoder with sensible overrides of default behaviors commit 0f275bf Author: Matt Hurne <matt@thehurnes.com> Date: Tue Oct 8 13:18:26 2013 -0400 Unwrap RuntimeJsonMappingExceptions caught in JacksonDecoder, since they are only ever used to wrap JsonMappingExceptions, which are IOExceptions. commit 1b69952 Author: Matt Hurne <matt@thehurnes.com> Date: Tue Oct 8 13:09:44 2013 -0400 Update Jackson integration README commit add4007 Author: Matt Hurne <matt@thehurnes.com> Date: Tue Oct 8 13:07:35 2013 -0400 Update CHANGES and README to reflect addition of Jackson integration commit 86c0fcf Author: Matt Hurne <matt@thehurnes.com> Date: Tue Oct 8 12:11:56 2013 -0400 Update Jackson GitHub example to make use of JacksonModule, and to avoid the need for Jackson annotations commit 1552b3f Author: Matt Hurne <matt@thehurnes.com> Date: Tue Oct 8 12:05:56 2013 -0400 Replace wildcard import with individual imports commit 0b7cfd0 Author: Matt Hurne <matt@thehurnes.com> Date: Tue Oct 8 11:01:11 2013 -0400 Initial implementation of Jackson codec This new codec may be used as an alternative to Gson. commit 94027ec Author: Matt Hurne <matt@thehurnes.com> Date: Tue Oct 8 08:31:14 2013 -0400 Improve EncodeException and DecodeException Javadoc comments
1 parent a08b17a commit c92025c

12 files changed

Lines changed: 491 additions & 4 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Version 5.4.0
22
* Add `BasicAuthRequestInterceptor`
3+
* Add Jackson integration
34

45
### Version 5.3.0
56
* Split `GsonCodec` into `GsonEncoder` and `GsonDecoder`, which are easy to use with `Feign.Builder`

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ GitHub github = Feign.builder()
9999
.target(GitHub.class, "https://api.github.com");
100100
```
101101

102+
### Jackson
103+
[JacksonModule](https://github.com/Netflix/feign/tree/master/jackson) adds an encoder and decoder you can use with a JSON API.
104+
105+
Add `JacksonEncoder` and/or `JacksonDecoder` to your `Feign.Builder` like so:
106+
107+
```java
108+
GitHub github = Feign.builder()
109+
.encoder(new JacksonEncoder())
110+
.decoder(new JacksonDecoder())
111+
.target(GitHub.class, "https://api.github.com");
112+
```
113+
102114
### Sax
103115
[SaxDecoder](https://github.com/Netflix/feign/tree/master/sax) allows you to decode XML in a way that is compatible with normal JVM and also Android environments.
104116

build.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ project(':feign-gson') {
7373
}
7474
}
7575

76+
project(':feign-jackson') {
77+
apply plugin: 'java'
78+
79+
test {
80+
useTestNG()
81+
}
82+
83+
dependencies {
84+
compile project(':feign-core')
85+
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.2'
86+
testCompile 'org.testng:testng:6.8.5'
87+
testCompile 'com.google.guava:guava:14.0.1'
88+
}
89+
}
90+
7691
project(':feign-jaxrs') {
7792
apply plugin: 'java'
7893

core/src/main/java/feign/codec/DecodeException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/**
2323
* Similar to {@code javax.websocket.DecodeException}, raised when a problem
2424
* occurs decoding a message. Note that {@code DecodeException} is not an
25-
* {@code IOException}, nor have one set as its cause.
25+
* {@code IOException}, nor does it have one set as its cause.
2626
*/
2727
public class DecodeException extends FeignException {
2828

core/src/main/java/feign/codec/EncodeException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
/**
2323
* Similar to {@code javax.websocket.EncodeException}, raised when a problem
24-
* occurs decoding a message. Note that {@code DecodeException} is not an
25-
* {@code IOException}, nor have one set as its cause.
24+
* occurs encoding a message. Note that {@code EncodeException} is not an
25+
* {@code IOException}, nor does it have one set as its cause.
2626
*/
2727
public class EncodeException extends FeignException {
2828

jackson/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Jackson Codec
2+
===================
3+
4+
This module adds support for encoding and decoding JSON via Jackson.
5+
6+
Add `JacksonEncoder` and/or `JacksonDecoder` to your `Feign.Builder` like so:
7+
8+
```java
9+
GitHub github = Feign.builder()
10+
.encoder(new JacksonEncoder())
11+
.decoder(new JacksonDecoder())
12+
.target(GitHub.class, "https://api.github.com");
13+
```
14+
15+
If you want to customize the `ObjectMapper` that is used, provide it to the `JacksonEncoder` and `JacksonDecoder`:
16+
17+
```java
18+
ObjectMapper mapper = new ObjectMapper()
19+
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
20+
.configure(SerializationFeature.INDENT_OUTPUT, true)
21+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
22+
23+
GitHub github = Feign.builder()
24+
.encoder(new JacksonEncoder(mapper))
25+
.decoder(new JacksonDecoder(mapper))
26+
.target(GitHub.class, "https://api.github.com");
27+
```
28+
29+
Alternatively, you can add the encoder and decoder to your Dagger object graph using the provided `JacksonModule` like so:
30+
31+
```java
32+
GitHub github = Feign.create(GitHub.class, "https://api.github.com", new JacksonModule());
33+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2013 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package feign.jackson;
17+
18+
import com.fasterxml.jackson.databind.DeserializationFeature;
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.fasterxml.jackson.databind.RuntimeJsonMappingException;
21+
import feign.Response;
22+
import feign.codec.Decoder;
23+
24+
import java.io.IOException;
25+
import java.io.Reader;
26+
import java.lang.reflect.Type;
27+
28+
public class JacksonDecoder implements Decoder {
29+
private final ObjectMapper mapper;
30+
31+
public JacksonDecoder() {
32+
this(new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false));
33+
}
34+
35+
public JacksonDecoder(ObjectMapper mapper) {
36+
this.mapper = mapper;
37+
}
38+
39+
@Override public Object decode(Response response, Type type) throws IOException {
40+
if (response.body() == null) {
41+
return null;
42+
}
43+
Reader reader = response.body().asReader();
44+
try {
45+
return mapper.readValue(reader, mapper.constructType(type));
46+
} catch (RuntimeJsonMappingException e) {
47+
if (e.getCause() != null && e.getCause() instanceof IOException) {
48+
throw IOException.class.cast(e.getCause());
49+
}
50+
throw e;
51+
}
52+
}
53+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2013 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package feign.jackson;
17+
18+
import com.fasterxml.jackson.annotation.JsonInclude;
19+
import com.fasterxml.jackson.core.JsonProcessingException;
20+
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import com.fasterxml.jackson.databind.SerializationFeature;
22+
import feign.RequestTemplate;
23+
import feign.codec.EncodeException;
24+
import feign.codec.Encoder;
25+
26+
public class JacksonEncoder implements Encoder {
27+
private final ObjectMapper mapper;
28+
29+
public JacksonEncoder() {
30+
this(new ObjectMapper()
31+
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
32+
.configure(SerializationFeature.INDENT_OUTPUT, true));
33+
}
34+
35+
public JacksonEncoder(ObjectMapper mapper) {
36+
this.mapper = mapper;
37+
}
38+
39+
@Override public void encode(Object object, RequestTemplate template) throws EncodeException {
40+
try {
41+
template.body(mapper.writeValueAsString(object));
42+
} catch (JsonProcessingException e) {
43+
throw new EncodeException(e.getMessage(), e);
44+
}
45+
}
46+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2013 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package feign.jackson;
17+
18+
import com.fasterxml.jackson.annotation.JsonInclude;
19+
import com.fasterxml.jackson.databind.DeserializationFeature;
20+
import com.fasterxml.jackson.databind.JsonDeserializer;
21+
import com.fasterxml.jackson.databind.JsonSerializer;
22+
import com.fasterxml.jackson.databind.Module;
23+
import com.fasterxml.jackson.databind.ObjectMapper;
24+
import com.fasterxml.jackson.databind.SerializationFeature;
25+
import dagger.Provides;
26+
import feign.Feign;
27+
import feign.codec.Decoder;
28+
import feign.codec.Encoder;
29+
30+
import javax.inject.Singleton;
31+
import java.util.Collections;
32+
import java.util.Set;
33+
34+
/**
35+
* <h3>Custom serializers/deserializers</h3>
36+
* <br>
37+
* In order to specify custom json parsing, Jackson's {@code ObjectMapper} supports {@link JsonSerializer serializers}
38+
* and {@link JsonDeserializer deserializers}, which can be bundled together in {@link Module modules}.
39+
* <p/>
40+
* <br>
41+
* Here's an example of adding a custom module.
42+
* <p/>
43+
* <pre>
44+
* public class ObjectIdSerializer extends StdSerializer&lt;ObjectId&gt; {
45+
* public ObjectIdSerializer() {
46+
* super(ObjectId.class);
47+
* }
48+
*
49+
* &#064;Override
50+
* public void serialize(ObjectId value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
51+
* jsonGenerator.writeString(value.toString());
52+
* }
53+
* }
54+
*
55+
* public class ObjectIdDeserializer extends StdDeserializer&lt;ObjectId&gt; {
56+
* public ObjectIdDeserializer() {
57+
* super(ObjectId.class);
58+
* }
59+
*
60+
* &#064;Override
61+
* public ObjectId deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
62+
* return ObjectId.massageToObjectId(jsonParser.getValueAsString());
63+
* }
64+
* }
65+
*
66+
* public class ObjectIdModule extends SimpleModule {
67+
* public ObjectIdModule() {
68+
* // first deserializers
69+
* addDeserializer(ObjectId.class, new ObjectIdDeserializer());
70+
*
71+
* // then serializers:
72+
* addSerializer(ObjectId.class, new ObjectIdSerializer());
73+
* }
74+
* }
75+
*
76+
* &#064;Provides(type = Provides.Type.SET)
77+
* Module objectIdModule() {
78+
* return new ObjectIdModule();
79+
* }
80+
* </pre>
81+
*/
82+
@dagger.Module(injects = Feign.class, addsTo = Feign.Defaults.class)
83+
public final class JacksonModule {
84+
@Provides Encoder encoder(ObjectMapper mapper) {
85+
return new JacksonEncoder(mapper);
86+
}
87+
88+
@Provides Decoder decoder(ObjectMapper mapper) {
89+
return new JacksonDecoder(mapper);
90+
}
91+
92+
@Provides @Singleton ObjectMapper mapper(Set<Module> modules) {
93+
return new ObjectMapper()
94+
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
95+
.configure(SerializationFeature.INDENT_OUTPUT, true)
96+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
97+
.registerModules(modules);
98+
}
99+
100+
@Provides(type = Provides.Type.SET_VALUES) Set<Module> noDefaultModules() {
101+
return Collections.emptySet();
102+
}
103+
}

0 commit comments

Comments
 (0)