Skip to content

Commit 8b7a4f7

Browse files
julien-pinta-pivotianardee
authored andcommitted
🐛 Use new style, static object mapper to avoid recreating multiple times
1 parent e3968f1 commit 8b7a4f7

File tree

3 files changed

+5
-14
lines changed

3 files changed

+5
-14
lines changed

src/main/java/com/mindee/http/MindeeHttpApi.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.JavaType;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.json.JsonMapper;
67
import com.mindee.MindeeException;
78
import com.mindee.MindeeSettings;
89
import com.mindee.parsing.common.ApiResponse;
@@ -40,7 +41,7 @@
4041
*/
4142
public final class MindeeHttpApi extends MindeeApi {
4243

43-
private static final ObjectMapper mapper = new ObjectMapper();
44+
private static final ObjectMapper mapper = JsonMapper.builder().findAndAddModules().build();
4445
private final Function<Endpoint, String> buildProductPredicBasetUrl = this::buildProductPredictBaseUrl;
4546
private final Function<String, String> buildWorkflowPredictBaseUrl = this::buildWorkflowPredictBaseUrl;
4647
private final Function<String, String> buildWorkflowExecutionBaseUrl = this::buildWorkflowExecutionUrl;
@@ -146,8 +147,6 @@ public <DocT extends Inference> AsyncPredictResponse<DocT> documentQueueGet(
146147
String endpointUrl = documentUrlFromEndpoint.apply(endpoint).concat(jobId);
147148
HttpGet get = new HttpGet(endpointUrl);
148149

149-
// required to register jackson date module format to deserialize
150-
mapper.findAndRegisterModules();
151150
JavaType parametricType = mapper
152151
.getTypeFactory()
153152
.constructParametricType(AsyncPredictResponse.class, documentClass);
@@ -198,8 +197,6 @@ public <DocT extends Inference> PredictResponse<DocT> predictPost(
198197
String url = urlFromEndpoint.apply(endpoint);
199198
HttpPost post = buildHttpPost(url, requestParameters);
200199

201-
// required to register jackson date module format to deserialize
202-
mapper.findAndRegisterModules();
203200
JavaType parametricType = mapper
204201
.getTypeFactory()
205202
.constructParametricType(PredictResponse.class, documentClass);
@@ -240,8 +237,6 @@ public <DocT extends Inference> AsyncPredictResponse<DocT> predictAsyncPost(
240237
}
241238
HttpPost post = buildHttpPost(url, requestParameters);
242239

243-
// required to register jackson date module format to deserialize
244-
mapper.findAndRegisterModules();
245240
JavaType parametricType = mapper
246241
.getTypeFactory()
247242
.constructParametricType(AsyncPredictResponse.class, documentClass);
@@ -277,8 +272,6 @@ public <DocT extends Inference> WorkflowResponse<DocT> executeWorkflowPost(
277272
String url = workflowUrlFromId.apply(workflowId);
278273
HttpPost post = buildHttpPost(url, requestParameters);
279274

280-
// required to register jackson date module format to deserialize
281-
mapper.findAndRegisterModules();
282275
JavaType parametricType = mapper
283276
.getTypeFactory()
284277
.constructParametricType(WorkflowResponse.class, documentClass);

src/main/java/com/mindee/http/MindeeHttpApiV2.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mindee.http;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.json.JsonMapper;
45
import com.mindee.MindeeException;
56
import com.mindee.MindeeSettingsV2;
67
import com.mindee.input.LocalInputSource;
@@ -33,7 +34,7 @@
3334
*/
3435
public final class MindeeHttpApiV2 extends MindeeApiV2 {
3536

36-
private static final ObjectMapper mapper = new ObjectMapper();
37+
private static final ObjectMapper mapper = JsonMapper.builder().findAndAddModules().build();
3738

3839
/**
3940
* The MindeeSetting needed to make the api call.
@@ -115,7 +116,6 @@ public JobResponse reqPostEnqueue(URLInputSource inputSource, BaseParameters opt
115116
* @return a valid job response.
116117
*/
117118
private JobResponse executeEnqueue(HttpPost post) {
118-
mapper.findAndRegisterModules();
119119
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
120120
return httpClient.execute(post, response -> {
121121
HttpEntity responseEntity = response.getEntity();
@@ -149,7 +149,6 @@ public JobResponse reqGetJob(String jobId) {
149149
RequestConfig noRedirect = RequestConfig.custom().setRedirectsEnabled(false).build();
150150
get.setConfig(noRedirect);
151151

152-
mapper.findAndRegisterModules();
153152
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
154153
return httpClient.execute(get, response -> {
155154
HttpEntity responseEntity = response.getEntity();
@@ -190,8 +189,6 @@ public <TResponse extends CommonResponse> TResponse reqGetResult(
190189
}
191190
get.setHeader(HttpHeaders.USER_AGENT, getUserAgent());
192191

193-
mapper.findAndRegisterModules();
194-
195192
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
196193

197194
return httpClient.execute(get, response -> {

src/main/java/com/mindee/input/LocalResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
@Getter
2727
public class LocalResponse {
2828
private final byte[] file;
29+
private static final ObjectMapper mapper = new ObjectMapper();
2930

3031
/**
3132
* Load from an {@link InputStream}.

0 commit comments

Comments
 (0)