Skip to content

Commit 537d22e

Browse files
committed
#30 - ENH: Add default constructor option for JacksonBodyAdapter() with reasonable defaults
1 parent 6edef86 commit 537d22e

File tree

6 files changed

+21
-20
lines changed

6 files changed

+21
-20
lines changed

http-client/client/src/main/java/io/avaje/http/client/JacksonBodyAdapter.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package io.avaje.http.client;
22

3+
import com.fasterxml.jackson.annotation.JsonInclude;
34
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import com.fasterxml.jackson.databind.ObjectReader;
6-
import com.fasterxml.jackson.databind.ObjectWriter;
5+
import com.fasterxml.jackson.databind.*;
76
import com.fasterxml.jackson.databind.type.CollectionType;
87

98
import java.io.IOException;
@@ -18,7 +17,6 @@
1817
* HttpClientContext.newBuilder()
1918
* .baseUrl(baseUrl)
2019
* .bodyAdapter(new JacksonBodyAdapter(new ObjectMapper()))
21-
* //.bodyAdapter(new GsonBodyAdapter(new Gson()))
2220
* .build();
2321
*
2422
* }</pre>
@@ -33,10 +31,24 @@ public class JacksonBodyAdapter implements BodyAdapter {
3331

3432
private final ConcurrentHashMap<Class<?>, BodyReader<?>> listReaderCache = new ConcurrentHashMap<>();
3533

34+
/**
35+
* Create passing the ObjectMapper to use.
36+
*/
3637
public JacksonBodyAdapter(ObjectMapper mapper) {
3738
this.mapper = mapper;
3839
}
3940

41+
/**
42+
* Create with a ObjectMapper that allows unknown properties and inclusion non empty.
43+
*/
44+
public JacksonBodyAdapter() {
45+
this.mapper = new ObjectMapper()
46+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
47+
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
48+
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
49+
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
50+
}
51+
4052
@Override
4153
public BodyWriter beanWriter(Class<?> cls) {
4254
return beanWriterCache.computeIfAbsent(cls, aClass -> {

http-client/client/src/test/java/io/avaje/http/client/DHttpApiTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ public class DHttpApiTest {
1616
@Test
1717
void test_github_listRepos() {
1818

19-
JacksonBodyAdapter bodyAdapter = new JacksonBodyAdapter(new ObjectMapper()
20-
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false));
21-
2219
final HttpClientContext clientContext = HttpClientContext.newBuilder()
2320
.baseUrl("https://api.github.com")
24-
.bodyAdapter(bodyAdapter)
21+
.bodyAdapter(new JacksonBodyAdapter())
2522
.build();
2623

2724
DHttpApi httpApi = new DHttpApi();

http-client/client/src/test/java/io/avaje/http/client/RequestListenerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private HttpClientContext createClient(TDRequestListener tdRequestListener) {
3737
return HttpClientContext.newBuilder()
3838
.baseUrl(baseUrl)
3939
.requestListener(new RequestLogger())
40-
.bodyAdapter(new JacksonBodyAdapter(new ObjectMapper()))
40+
.bodyAdapter(new JacksonBodyAdapter())
4141
.requestListener(tdRequestListener)
4242
.build();
4343
}

http-client/client/src/test/java/io/avaje/http/client/RetryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class RetryTest extends BaseWebTest {
1515
HttpClientContext initClientWithRetry() {
1616
return HttpClientContext.newBuilder()
1717
.baseUrl("http://localhost:8887")
18-
.bodyAdapter(new JacksonBodyAdapter(new ObjectMapper()))
18+
.bodyAdapter(new JacksonBodyAdapter())
1919
.requestListener(new RequestLogger())
2020
.retryHandler(new SimpleRetryHandler(4, 1))
2121
.requestIntercept(myIntercept)

http-client/client/src/test/java/org/example/github/GithubTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.example.github;
22

3-
import com.fasterxml.jackson.databind.DeserializationFeature;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
53
import io.avaje.http.client.HttpClientContext;
64
import io.avaje.http.client.JacksonBodyAdapter;
75
import io.avaje.http.client.RequestLogger;
@@ -14,16 +12,13 @@
1412

1513
public class GithubTest {
1614

17-
private final JacksonBodyAdapter bodyAdapter = new JacksonBodyAdapter(new ObjectMapper()
18-
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false));
19-
2015
@Test
2116
@Disabled
2217
void test() throws InterruptedException {
2318

2419
final HttpClientContext clientContext = HttpClientContext.newBuilder()
2520
.baseUrl("https://api.github.com")
26-
.bodyAdapter(bodyAdapter)
21+
.bodyAdapter(new JacksonBodyAdapter())
2722
.requestListener(new RequestLogger())
2823
.build();
2924

http-client/test/src/test/java/example/github/GithubTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package example.github;
22

3-
import com.fasterxml.jackson.databind.DeserializationFeature;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
53
import com.google.gson.Gson;
64
import io.avaje.http.client.BodyAdapter;
75
import io.avaje.http.client.HttpClientContext;
@@ -40,8 +38,7 @@ private void assertListRepos(BodyAdapter bodyAdapter) {
4038
}
4139

4240
private BodyAdapter jacksonBodyAdapter() {
43-
return new JacksonBodyAdapter(new ObjectMapper()
44-
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false));
41+
return new JacksonBodyAdapter();
4542
}
4643

4744
private BodyAdapter gsonBodyAdapter() {

0 commit comments

Comments
 (0)