1
1
package com .softdev .system .generator .config ;
2
2
3
- import com .alibaba .fastjson .support .config .FastJsonConfig ;
4
- import com .alibaba .fastjson .support .spring .FastJsonHttpMessageConverter ;
3
+
4
+ // import com.alibaba.fastjson.support.config.FastJsonConfig;
5
+ // import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
5
6
import jakarta .servlet .DispatcherType ;
6
7
import org .springframework .boot .web .servlet .FilterRegistrationBean ;
7
8
import org .springframework .context .annotation .Bean ;
12
13
import org .springframework .web .servlet .config .annotation .ResourceHandlerRegistry ;
13
14
import org .springframework .web .servlet .config .annotation .WebMvcConfigurer ;
14
15
16
+ import com .alibaba .fastjson2 .JSONReader ;
17
+ import com .alibaba .fastjson2 .JSONWriter ;
18
+ import com .alibaba .fastjson2 .support .config .FastJsonConfig ;
19
+ import com .alibaba .fastjson2 .support .spring6 .http .converter .FastJsonHttpMessageConverter ;
20
+
15
21
import java .nio .charset .StandardCharsets ;
16
22
import java .util .ArrayList ;
23
+ import java .util .Collections ;
17
24
import java .util .List ;
18
25
/**
19
26
* 2019-2-11 liutf WebMvcConfig 整合 cors 和 SpringMvc MessageConverter
@@ -36,25 +43,43 @@ public FilterRegistrationBean xssFilterRegistration() {
36
43
return registration ;
37
44
}
38
45
46
+ // @Override
47
+ // public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
48
+ // converters.clear();
49
+ // //FastJsonHttpMessageConverter
50
+ // FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
51
+
52
+ // List<MediaType> fastMediaTypes = new ArrayList<>();
53
+ // fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
54
+ // fastConverter.setSupportedMediaTypes(fastMediaTypes);
55
+
56
+ // FastJsonConfig fastJsonConfig = new FastJsonConfig();
57
+ // fastJsonConfig.setCharset(StandardCharsets.UTF_8);
58
+ // fastConverter.setFastJsonConfig(fastJsonConfig);
59
+
60
+ // //StringHttpMessageConverter
61
+ // StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
62
+ // stringConverter.setDefaultCharset(StandardCharsets.UTF_8);
63
+ // stringConverter.setSupportedMediaTypes(fastMediaTypes);
64
+ // converters.add(stringConverter);
65
+ // converters.add(fastConverter);
66
+ // }
67
+ /**
68
+ * FASTJSON2升级 by https://zhengkai.blog.csdn.net/
69
+ * https://blog.csdn.net/moshowgame/article/details/138013669
70
+ */
39
71
@ Override
40
72
public void configureMessageConverters (List <HttpMessageConverter <?>> converters ) {
41
- //FastJsonHttpMessageConverter
42
- FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter ();
43
-
44
- List <MediaType > fastMediaTypes = new ArrayList <>();
45
- fastMediaTypes .add (MediaType .APPLICATION_JSON_UTF8 );
46
- fastConverter .setSupportedMediaTypes (fastMediaTypes );
47
-
48
- FastJsonConfig fastJsonConfig = new FastJsonConfig ();
49
- fastJsonConfig .setCharset (StandardCharsets .UTF_8 );
50
- fastConverter .setFastJsonConfig (fastJsonConfig );
51
-
52
- //StringHttpMessageConverter
53
- StringHttpMessageConverter stringConverter = new StringHttpMessageConverter ();
54
- stringConverter .setDefaultCharset (StandardCharsets .UTF_8 );
55
- stringConverter .setSupportedMediaTypes (fastMediaTypes );
56
- converters .add (stringConverter );
57
- converters .add (fastConverter );
73
+ FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter ();
74
+ //自定义配置...
75
+ FastJsonConfig config = new FastJsonConfig ();
76
+ config .setDateFormat ("yyyy-MM-dd HH:mm:ss" );
77
+ config .setReaderFeatures (JSONReader .Feature .FieldBased , JSONReader .Feature .SupportArrayToBean );
78
+ config .setWriterFeatures (JSONWriter .Feature .WriteMapNullValue , JSONWriter .Feature .PrettyFormat );
79
+ converter .setFastJsonConfig (config );
80
+ converter .setDefaultCharset (StandardCharsets .UTF_8 );
81
+ converter .setSupportedMediaTypes (Collections .singletonList (MediaType .APPLICATION_JSON ));
82
+ converters .add (0 , converter );
58
83
}
59
84
60
85
}
0 commit comments