@@ -65,8 +65,6 @@ public class MessageBirdServiceImpl implements MessageBirdService {
65
65
private final String userAgentString ;
66
66
private Proxy proxy = null ;
67
67
68
- private final ObjectMapper mapper ;
69
-
70
68
public MessageBirdServiceImpl (final String accessKey , final String serviceUrl ) {
71
69
if (accessKey == null ) {
72
70
throw new IllegalArgumentException (ACCESS_KEY_MUST_BE_SPECIFIED );
@@ -78,16 +76,6 @@ public MessageBirdServiceImpl(final String accessKey, final String serviceUrl) {
78
76
this .serviceUrl = serviceUrl ;
79
77
this .userAgentString = determineUserAgentString ();
80
78
81
- this .mapper = new ObjectMapper ()
82
- // If we as new properties, we don't want the system to fail, we rather want to ignore them
83
- .disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES )
84
- // Enable case insensitivity to avoid parsing errors if parameters' case in api response doesn't match sdk's
85
- .enable (MapperFeature .ACCEPT_CASE_INSENSITIVE_PROPERTIES , MapperFeature .ACCEPT_CASE_INSENSITIVE_ENUMS )
86
- .setSerializationInclusion (Include .NON_NULL )
87
- // Specifically set the date format for POST requests so scheduled
88
- // messages and other things relying on specific date formats don't
89
- // fail when sending.
90
- .setDateFormat (getDateFormat ());
91
79
}
92
80
93
81
private String determineUserAgentString () {
@@ -231,6 +219,13 @@ public <T, P> T getJsonData(final String request, final P payload, final String
231
219
232
220
if (status == HttpURLConnection .HTTP_OK || status == HttpURLConnection .HTTP_CREATED ) {
233
221
try {
222
+ final ObjectMapper mapper = new ObjectMapper ();
223
+ // If we as new properties, we don't want the system to fail, we rather want to ignore them
224
+ mapper .disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
225
+ // Enable case insensitivity to avoid parsing errors if parameters' case in api response doesn't match sdk's
226
+ mapper .enable (MapperFeature .ACCEPT_CASE_INSENSITIVE_PROPERTIES );
227
+ mapper .enable (MapperFeature .ACCEPT_CASE_INSENSITIVE_ENUMS );
228
+
234
229
return mapper .readValue (body , clazz );
235
230
} catch (IOException ioe ) {
236
231
throw new GeneralException (ioe );
@@ -482,6 +477,13 @@ public <P> HttpURLConnection getConnection(final String serviceUrl, final P body
482
477
connection .setRequestMethod (requestType );
483
478
connection .setDoOutput (true );
484
479
connection .setRequestProperty ("Content-Type" , "application/json" );
480
+ ObjectMapper mapper = new ObjectMapper ();
481
+ mapper .setSerializationInclusion (Include .NON_NULL );
482
+ // Specifically set the date format for POST requests so scheduled
483
+ // messages and other things relying on specific date formats don't
484
+ // fail when sending.
485
+ DateFormat df = getDateFormat ();
486
+ mapper .setDateFormat (df );
485
487
486
488
final String json = mapper .writeValueAsString (body );
487
489
connection .getOutputStream ().write (json .getBytes (String .valueOf (StandardCharsets .UTF_8 )));
@@ -535,13 +537,14 @@ private double getVersion() throws GeneralException {
535
537
* @return Error report, or null if the body can not be deserialized.
536
538
*/
537
539
private List <ErrorReport > getErrorReportOrNull (final String body ) {
540
+ ObjectMapper objectMapper = new ObjectMapper ();
538
541
try {
539
- JsonNode jsonNode = mapper .readValue (body , JsonNode .class );
542
+ JsonNode jsonNode = objectMapper .readValue (body , JsonNode .class );
540
543
if (!jsonNode .has ("errors" )) {
541
544
return null ;
542
545
}
543
546
544
- ErrorReport [] errors = mapper .readValue (jsonNode .get ("errors" ).toString (), ErrorReport [].class );
547
+ ErrorReport [] errors = objectMapper .readValue (jsonNode .get ("errors" ).toString (), ErrorReport [].class );
545
548
546
549
List <ErrorReport > result = Arrays .asList (errors );
547
550
0 commit comments