1616
1717package org .springframework .boot .http .codec .autoconfigure ;
1818
19+ import com .google .gson .Gson ;
20+ import kotlinx .serialization .Serializable ;
21+ import kotlinx .serialization .json .Json ;
1922import org .jspecify .annotations .Nullable ;
20- import tools .jackson .databind .ObjectMapper ;
2123import tools .jackson .databind .json .JsonMapper ;
2224
2325import org .springframework .boot .autoconfigure .AutoConfiguration ;
2426import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
2527import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
2628import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
29+ import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
2730import org .springframework .boot .context .properties .EnableConfigurationProperties ;
2831import org .springframework .boot .context .properties .PropertyMapper ;
2932import org .springframework .boot .http .codec .CodecCustomizer ;
3235import org .springframework .core .Ordered ;
3336import org .springframework .core .annotation .Order ;
3437import org .springframework .http .codec .CodecConfigurer ;
38+ import org .springframework .http .codec .CodecConfigurer .CustomCodecs ;
39+ import org .springframework .http .codec .json .GsonDecoder ;
40+ import org .springframework .http .codec .json .GsonEncoder ;
3541import org .springframework .http .codec .json .JacksonJsonDecoder ;
3642import org .springframework .http .codec .json .JacksonJsonEncoder ;
43+ import org .springframework .http .codec .json .KotlinSerializationJsonDecoder ;
44+ import org .springframework .http .codec .json .KotlinSerializationJsonEncoder ;
3745import org .springframework .util .unit .DataSize ;
3846import org .springframework .web .reactive .function .client .WebClient ;
3947
4351 * {@link org.springframework.core.codec.Decoder Decoders}.
4452 *
4553 * @author Brian Clozel
54+ * @author Vasily Pelikh
4655 * @since 2.0.0
4756 */
48- @ AutoConfiguration (afterName = "org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration" )
57+ @ AutoConfiguration (afterName = { "org.springframework.boot.jackson.autoconfigure.JacksonAutoConfiguration" ,
58+ "org.springframework.boot.gson.autoconfigure.GsonAutoConfiguration" ,
59+ "org.springframework.boot.kotlin.serialization.autoconfigure.KotlinSerializationAutoConfiguration" })
4960@ ConditionalOnClass ({ CodecConfigurer .class , WebClient .class })
5061public final class CodecsAutoConfiguration {
5162
63+ private static final String PREFERRED_MAPPER_PROPERTY = "spring.http.codecs.preferred-json-mapper" ;
64+
5265 @ Configuration (proxyBeanMethods = false )
53- @ ConditionalOnClass (ObjectMapper .class )
66+ @ ConditionalOnClass (JsonMapper .class )
67+ @ ConditionalOnBean (JsonMapper .class )
68+ @ ConditionalOnProperty (name = PREFERRED_MAPPER_PROPERTY , havingValue = "jackson" , matchIfMissing = true )
5469 static class JacksonJsonCodecConfiguration {
5570
5671 @ Bean
5772 @ Order (0 )
58- @ ConditionalOnBean (JsonMapper .class )
5973 CodecCustomizer jacksonCodecCustomizer (JsonMapper jsonMapper ) {
6074 return (configurer ) -> {
6175 CodecConfigurer .DefaultCodecs defaults = configurer .defaultCodecs ();
@@ -66,6 +80,41 @@ CodecCustomizer jacksonCodecCustomizer(JsonMapper jsonMapper) {
6680
6781 }
6882
83+ @ Configuration (proxyBeanMethods = false )
84+ @ ConditionalOnClass (Gson .class )
85+ @ ConditionalOnBean (Gson .class )
86+ @ ConditionalOnProperty (name = PREFERRED_MAPPER_PROPERTY , havingValue = "gson" )
87+ static class GsonJsonCodecConfiguration {
88+
89+ @ Bean
90+ CodecCustomizer gsonCodecCustomizer (Gson gson ) {
91+ return (configurer ) -> {
92+ CustomCodecs customCodecs = configurer .customCodecs ();
93+ customCodecs .registerWithDefaultConfig (new GsonDecoder (gson ));
94+ customCodecs .registerWithDefaultConfig (new GsonEncoder (gson ));
95+ };
96+ }
97+
98+ }
99+
100+ @ Configuration (proxyBeanMethods = false )
101+ @ ConditionalOnClass ({ Serializable .class , Json .class })
102+ @ ConditionalOnBean (Json .class )
103+ @ ConditionalOnProperty (name = PREFERRED_MAPPER_PROPERTY , havingValue = "kotlin-serialization" )
104+ static class KotlinSerializationJsonCodecConfiguration {
105+
106+ @ Bean
107+ @ Order (-10 ) // configured ahead of JSON mappers
108+ CodecCustomizer kotlinSerializationCodecCustomizer (Json json ) {
109+ return (configurer ) -> {
110+ CustomCodecs customCodecs = configurer .customCodecs ();
111+ customCodecs .registerWithDefaultConfig (new KotlinSerializationJsonDecoder (json ));
112+ customCodecs .registerWithDefaultConfig (new KotlinSerializationJsonEncoder (json ));
113+ };
114+ }
115+
116+ }
117+
69118 @ Configuration (proxyBeanMethods = false )
70119 @ EnableConfigurationProperties (HttpCodecsProperties .class )
71120 static class DefaultCodecsConfiguration {
0 commit comments