16
16
17
17
package org .springframework .boot .http .codec .autoconfigure ;
18
18
19
+ import com .google .gson .Gson ;
20
+ import kotlinx .serialization .Serializable ;
21
+ import kotlinx .serialization .json .Json ;
19
22
import org .jspecify .annotations .Nullable ;
20
- import tools .jackson .databind .ObjectMapper ;
21
23
import tools .jackson .databind .json .JsonMapper ;
22
24
23
25
import org .springframework .boot .autoconfigure .AutoConfiguration ;
24
26
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
25
27
import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
26
28
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
29
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
27
30
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
28
31
import org .springframework .boot .context .properties .PropertyMapper ;
29
32
import org .springframework .boot .http .codec .CodecCustomizer ;
32
35
import org .springframework .core .Ordered ;
33
36
import org .springframework .core .annotation .Order ;
34
37
import 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 ;
35
41
import org .springframework .http .codec .json .JacksonJsonDecoder ;
36
42
import org .springframework .http .codec .json .JacksonJsonEncoder ;
43
+ import org .springframework .http .codec .json .KotlinSerializationJsonDecoder ;
44
+ import org .springframework .http .codec .json .KotlinSerializationJsonEncoder ;
37
45
import org .springframework .util .unit .DataSize ;
38
46
import org .springframework .web .reactive .function .client .WebClient ;
39
47
43
51
* {@link org.springframework.core.codec.Decoder Decoders}.
44
52
*
45
53
* @author Brian Clozel
54
+ * @author Vasily Pelikh
46
55
* @since 2.0.0
47
56
*/
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" })
49
60
@ ConditionalOnClass ({ CodecConfigurer .class , WebClient .class })
50
61
public final class CodecsAutoConfiguration {
51
62
63
+ private static final String PREFERRED_MAPPER_PROPERTY = "spring.http.codecs.preferred-json-mapper" ;
64
+
52
65
@ 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 )
54
69
static class JacksonJsonCodecConfiguration {
55
70
56
71
@ Bean
57
72
@ Order (0 )
58
- @ ConditionalOnBean (JsonMapper .class )
59
73
CodecCustomizer jacksonCodecCustomizer (JsonMapper jsonMapper ) {
60
74
return (configurer ) -> {
61
75
CodecConfigurer .DefaultCodecs defaults = configurer .defaultCodecs ();
@@ -66,6 +80,41 @@ CodecCustomizer jacksonCodecCustomizer(JsonMapper jsonMapper) {
66
80
67
81
}
68
82
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
+
69
118
@ Configuration (proxyBeanMethods = false )
70
119
@ EnableConfigurationProperties (HttpCodecsProperties .class )
71
120
static class DefaultCodecsConfiguration {
0 commit comments