@@ -65,6 +65,8 @@ public class MessageBirdServiceImpl implements MessageBirdService {
6565 private final String userAgentString ;
6666 private Proxy proxy = null ;
6767
68+ private final ObjectMapper mapper ;
69+
6870 public MessageBirdServiceImpl (final String accessKey , final String serviceUrl ) {
6971 if (accessKey == null ) {
7072 throw new IllegalArgumentException (ACCESS_KEY_MUST_BE_SPECIFIED );
@@ -75,6 +77,17 @@ public MessageBirdServiceImpl(final String accessKey, final String serviceUrl) {
7577 this .accessKey = accessKey ;
7678 this .serviceUrl = serviceUrl ;
7779 this .userAgentString = determineUserAgentString ();
80+
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 ());
7891 }
7992
8093 private String determineUserAgentString () {
@@ -217,14 +230,6 @@ public <T, P> T getJsonData(final String request, final P payload, final String
217230 final int status = apiResponse .getStatus ();
218231
219232 if (status == HttpURLConnection .HTTP_OK || status == HttpURLConnection .HTTP_CREATED ) {
220- final ObjectMapper mapper = new ObjectMapper ();
221-
222- // If we as new properties, we don't want the system to fail, we rather want to ignore them
223- mapper .disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
224- // Enable case insensitivity to avoid parsing errors if parameters' case in api response doesn't match sdk's
225- mapper .enable (MapperFeature .ACCEPT_CASE_INSENSITIVE_PROPERTIES );
226- mapper .enable (MapperFeature .ACCEPT_CASE_INSENSITIVE_ENUMS );
227-
228233 try {
229234 return mapper .readValue (body , clazz );
230235 } catch (IOException ioe ) {
@@ -477,14 +482,6 @@ public <P> HttpURLConnection getConnection(final String serviceUrl, final P body
477482 connection .setRequestMethod (requestType );
478483 connection .setDoOutput (true );
479484 connection .setRequestProperty ("Content-Type" , "application/json" );
480- ObjectMapper mapper = new ObjectMapper ();
481- mapper .setSerializationInclusion (Include .NON_NULL );
482-
483- // Specifically set the date format for POST requests so scheduled
484- // messages and other things relying on specific date formats don't
485- // fail when sending.
486- DateFormat df = getDateFormat ();
487- mapper .setDateFormat (df );
488485
489486 final String json = mapper .writeValueAsString (body );
490487 connection .getOutputStream ().write (json .getBytes (String .valueOf (StandardCharsets .UTF_8 )));
@@ -538,15 +535,13 @@ private double getVersion() throws GeneralException {
538535 * @return Error report, or null if the body can not be deserialized.
539536 */
540537 private List <ErrorReport > getErrorReportOrNull (final String body ) {
541- ObjectMapper objectMapper = new ObjectMapper ();
542-
543538 try {
544- JsonNode jsonNode = objectMapper .readValue (body , JsonNode .class );
539+ JsonNode jsonNode = mapper .readValue (body , JsonNode .class );
545540 if (!jsonNode .has ("errors" )) {
546541 return null ;
547542 }
548543
549- ErrorReport [] errors = objectMapper .readValue (jsonNode .get ("errors" ).toString (), ErrorReport [].class );
544+ ErrorReport [] errors = mapper .readValue (jsonNode .get ("errors" ).toString (), ErrorReport [].class );
550545
551546 List <ErrorReport > result = Arrays .asList (errors );
552547
0 commit comments