22
33package { {invokerPackage} };
44
5+ import { {jacksonPackage} }.databind.DeserializationFeature;
6+
7+ { {#useJackson3} }
8+ import { {jacksonPackage} }.core.JacksonException;
9+ import { {jacksonPackage} }.databind.json.JsonMapper;
10+ import org.springframework.http.codec.json.JacksonJsonDecoder;
11+ import org.springframework.http.codec.json.JacksonJsonEncoder;
12+ { {/useJackson3} }
13+ { {^useJackson3} }
514import com.fasterxml.jackson.core.JsonProcessingException;
6- import com.fasterxml.jackson.databind.DeserializationFeature;
715import com.fasterxml.jackson.databind.ObjectMapper;
816import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
17+ import org.springframework.http.codec.json.Jackson2JsonDecoder;
18+ import org.springframework.http.codec.json.Jackson2JsonEncoder;
19+ { {/useJackson3} }
920{ {#openApiNullable} }
1021import org.openapitools.jackson.nullable.JsonNullableModule;
1122{ {/openApiNullable} }
@@ -27,8 +38,6 @@ import org.springframework.http.client.ClientHttpRequestExecution;
2738import org.springframework.http.client.ClientHttpRequestInterceptor;
2839import org.springframework.http.client.ClientHttpResponse;
2940import org.springframework.http.client.reactive.ClientHttpRequest;
30- import org.springframework.http.codec.json.Jackson2JsonDecoder;
31- import org.springframework.http.codec.json.Jackson2JsonEncoder;
3241{ {#generateClientAsBean} }
3342import org.springframework.stereotype.Component;
3443{ {/generateClientAsBean} }
@@ -110,15 +119,20 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
110119
111120 protected final WebClient webClient;
112121 protected final DateFormat dateFormat;
113- protected final ObjectMapper objectMapper;
122+ { {^useJackson3} }
123+ protected final ObjectMapper mapper;
124+ { {/useJackson3} }
125+ { {#useJackson3} }
126+ protected final JsonMapper mapper;
127+ { {/useJackson3} }
114128
115129 protected Map<String , Authentication > authentications;
116130
117131
118132 public ApiClient() {
119133 this.dateFormat = createDefaultDateFormat();
120- this.objectMapper = createDefaultObjectMapper (this.dateFormat);
121- this.webClient = buildWebClient(this.objectMapper );
134+ this.mapper = createDefaultMapper (this.dateFormat);
135+ this.webClient = buildWebClient(this.mapper );
122136 this.init();
123137 }
124138
@@ -129,18 +143,18 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
129143 this(Optional.ofNullable(webClient).orElseGet(() -> buildWebClient()), createDefaultDateFormat());
130144 }
131145
132- public ApiClient(ObjectMapper mapper, DateFormat format) {
133- this(buildWebClient(mapper.copy() ), format);
146+ public ApiClient({ {#useJackson3 } }JsonMapper { {/useJackson3 } } { {^useJackson3 } } ObjectMapper{ {/useJackson3 } } mapper, DateFormat format) {
147+ this(buildWebClient(mapper), format);
134148 }
135149
136- public ApiClient(WebClient webClient, ObjectMapper mapper, DateFormat format) {
137- this(Optional.ofNullable(webClient).orElseGet(() -> buildWebClient(mapper.copy() )), format);
150+ public ApiClient(WebClient webClient, { {#useJackson3 } }JsonMapper { {/useJackson3 } } { {^useJackson3 } } ObjectMapper{ {/useJackson3 } } mapper, DateFormat format) {
151+ this(Optional.ofNullable(webClient).orElseGet(() -> buildWebClient(mapper)), format);
138152 }
139153
140154 protected ApiClient(WebClient webClient, DateFormat format) {
141155 this.webClient = webClient;
142156 this.dateFormat = format;
143- this.objectMapper = createDefaultObjectMapper (format);
157+ this.mapper = createDefaultMapper (format);
144158 this.init();
145159 }
146160
@@ -150,7 +164,21 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
150164 return dateFormat;
151165 }
152166
153- public static ObjectMapper createDefaultObjectMapper(@Nullable DateFormat dateFormat) {
167+ { {#useJackson3} }
168+ public static JsonMapper createDefaultMapper(@Nullable DateFormat dateFormat) {
169+ return JsonMapper.builder()
170+ .defaultDateFormat(dateFormat)
171+ {{#failOnUnknownProperties} }
172+ .enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
173+ { {/failOnUnknownProperties} }
174+ { {^failOnUnknownProperties} }
175+ .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
176+ { {/failOnUnknownProperties} }
177+ .build();
178+ }
179+ { {/useJackson3} }
180+ { {^useJackson3} }
181+ public static ObjectMapper createDefaultMapper(@Nullable DateFormat dateFormat) {
154182 if (null == dateFormat) {
155183 dateFormat = createDefaultDateFormat();
156184 }
@@ -164,6 +192,8 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
164192 { {/openApiNullable} }
165193 return mapper;
166194 }
195+ { {/useJackson3} }
196+
167197
168198 protected void init() {
169199 // Setup authentications (key: authentication name, value: authentication).
@@ -181,12 +211,18 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
181211 * @param mapper ObjectMapper used for serialize/deserialize
182212 * @return WebClient
183213 */
184- public static WebClient.Builder buildWebClientBuilder(ObjectMapper mapper) {
214+ public static WebClient.Builder buildWebClientBuilder({ {#useJackson3 } }JsonMapper { {/useJackson3 } } { {^useJackson3 } } ObjectMapper{ {/useJackson3 } } mapper) {
185215 ExchangeStrategies strategies = ExchangeStrategies
186216 .builder()
187217 .codecs(clientDefaultCodecsConfigurer -> {
218+ {{#useJackson3} }
219+ clientDefaultCodecsConfigurer.defaultCodecs().jacksonJsonEncoder(new JacksonJsonEncoder(mapper, MediaType.APPLICATION_JSON));
220+ clientDefaultCodecsConfigurer.defaultCodecs().jacksonJsonDecoder(new JacksonJsonDecoder(mapper, MediaType.APPLICATION_JSON));
221+ { {/useJackson3} }
222+ { {^useJackson3} }
188223 clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper, MediaType.APPLICATION_JSON));
189224 clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper, MediaType.APPLICATION_JSON));
225+ { {/useJackson3} }
190226 }).build();
191227 WebClient.Builder webClientBuilder = WebClient.builder().exchangeStrategies(strategies);
192228 return webClientBuilder;
@@ -197,15 +233,15 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
197233 * @return WebClient
198234 */
199235 public static WebClient.Builder buildWebClientBuilder() {
200- return buildWebClientBuilder(createDefaultObjectMapper (null));
236+ return buildWebClientBuilder(createDefaultMapper (null));
201237 }
202238
203239 /**
204240 * Build the WebClient used to make HTTP requests.
205241 * @param mapper ObjectMapper used for serialize/deserialize
206242 * @return WebClient
207243 */
208- public static WebClient buildWebClient(ObjectMapper mapper) {
244+ public static WebClient buildWebClient({ {#useJackson3 } }JsonMapper { {/useJackson3 } } { {^useJackson3 } } ObjectMapper{ {/useJackson3 } } mapper) {
209245 return buildWebClientBuilder(mapper).build();
210246 }
211247
@@ -214,7 +250,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
214250 * @return WebClient
215251 */
216252 public static WebClient buildWebClient() {
217- return buildWebClientBuilder(createDefaultObjectMapper (null)).build();
253+ return buildWebClientBuilder(createDefaultMapper (null)).build();
218254 }
219255
220256 /**
@@ -403,11 +439,11 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
403439 }
404440
405441 /**
406- * Get the ObjectMapper used to make HTTP requests.
407- * @return ObjectMapper objectMapper
442+ * Get the { {#useJackson3 } }JsonMapper { {/useJackson3 } } { {^useJackson3 } } ObjectMapper{ {/useJackson3 } } used to make HTTP requests.
443+ * @return { {#useJackson3 } }JsonMapper { {/useJackson3 } } { {^useJackson3 } } ObjectMapper{ {/useJackson3 } } mapper
408444 */
409- public ObjectMapper getObjectMapper () {
410- return objectMapper ;
445+ public { {#useJackson3 } }JsonMapper { {/useJackson3 } } { {^useJackson3 } } ObjectMapper{ {/useJackson3 } } get { {#useJackson3 } }JsonMapper { {/useJackson3 } } { {^useJackson3 } }ObjectMapper { {/useJackson3 } } () {
446+ return mapper ;
411447 }
412448
413449 /**
@@ -457,17 +493,17 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
457493 valueCollection = (Collection< ?> ) value;
458494 } else {
459495 try {
460- return parameterToMultiValueMap(collectionFormat, name, objectMapper .writeValueAsString(value));
461- } catch (JsonProcessingException e) {
496+ return parameterToMultiValueMap(collectionFormat, name, mapper .writeValueAsString(value));
497+ } catch ({ {#useJackson3 } }JacksonException { {/useJackson3 } } { {^useJackson3 } } JsonProcessingException{ {/useJackson3 } } e) {
462498 throw new RuntimeException(e);
463499 }
464500 }
465501
466502 List<String > values = new ArrayList<> ();
467503 for(Object o : valueCollection) {
468504 try {
469- values.add(objectMapper .writeValueAsString(o));
470- } catch (JsonProcessingException e) {
505+ values.add(mapper .writeValueAsString(o));
506+ } catch ({ {#useJackson3 } }JacksonException { {/useJackson3 } } { {^useJackson3 } } JsonProcessingException{ {/useJackson3 } } e) {
471507 throw new RuntimeException(e);
472508 }
473509 }
0 commit comments