|
15 | 15 | */
|
16 | 16 | package me.zhengjie.config;
|
17 | 17 |
|
| 18 | +import com.alibaba.fastjson.serializer.SerializerFeature; |
| 19 | +import com.alibaba.fastjson.support.config.FastJsonConfig; |
| 20 | +import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; |
18 | 21 | import org.springframework.context.annotation.Bean;
|
19 | 22 | import org.springframework.context.annotation.Configuration;
|
| 23 | +import org.springframework.http.MediaType; |
| 24 | +import org.springframework.http.converter.HttpMessageConverter; |
20 | 25 | import org.springframework.web.cors.CorsConfiguration;
|
21 | 26 | import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
22 | 27 | import org.springframework.web.filter.CorsFilter;
|
23 | 28 | import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
24 | 29 | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
25 | 30 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
26 | 31 |
|
| 32 | +import java.nio.charset.Charset; |
| 33 | +import java.nio.charset.StandardCharsets; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.List; |
| 36 | + |
27 | 37 | /**
|
28 | 38 | * WebMvcConfigurer
|
29 | 39 | *
|
@@ -62,4 +72,29 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
62 | 72 | registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
|
63 | 73 | registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
|
64 | 74 | }
|
| 75 | + |
| 76 | + @Override |
| 77 | + public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { |
| 78 | + FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); |
| 79 | + List<MediaType> supportMediaTypeList = new ArrayList<>(); |
| 80 | + supportMediaTypeList.add(MediaType.APPLICATION_JSON_UTF8); |
| 81 | + FastJsonConfig config = new FastJsonConfig(); |
| 82 | + config.setDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 83 | + config.setSerializerFeatures( |
| 84 | + SerializerFeature.DisableCircularReferenceDetect, |
| 85 | + //保留空的字段 |
| 86 | + SerializerFeature.WriteMapNullValue, |
| 87 | + //String null -> "" |
| 88 | + SerializerFeature.WriteNullStringAsEmpty, |
| 89 | + //Number null -> 0 |
| 90 | + SerializerFeature.WriteNullNumberAsZero, |
| 91 | + //List null-> [] |
| 92 | + SerializerFeature.WriteNullListAsEmpty, |
| 93 | + //Boolean null -> false |
| 94 | + SerializerFeature.WriteNullBooleanAsFalse); |
| 95 | + converter.setFastJsonConfig(config); |
| 96 | + converter.setSupportedMediaTypes(supportMediaTypeList); |
| 97 | + converter.setDefaultCharset(StandardCharsets.UTF_8); |
| 98 | + converters.add(converter); |
| 99 | + } |
65 | 100 | }
|
0 commit comments