開發過程中中文一直是???顯示,根據網上的帖子修改配置文件后也沒有效果。后來偶然間看到一篇文章,把fastjson改為默認序列化插件,加入后就沒有問題了。
1 package com.leenleda.ward.tv.admin.interceptor; 2 3 import com.alibaba.fastjson.serializer.SerializerFeature; 4 import com.alibaba.fastjson.support.config.FastJsonConfig; 5 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; 6 import com.leenleda.ward.tv.common.config.LeenledaConfig; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.context.annotation.Configuration; 9 import org.springframework.http.MediaType; 10 import org.springframework.http.converter.HttpMessageConverter; 11 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; 12 import org.springframework.web.servlet.config.annotation.EnableWebMvc; 13 import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 14 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 15 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 16 17 import javax.annotation.Resource; 18 import java.util.ArrayList; 19 import java.util.Arrays; 20 import java.util.List; 21 22 23 /** 24 * @Author: pengbenlei 25 * @Date: 2020/2/19 11:22 26 * @Description: 27 */ 28 @Configuration 29 @EnableWebMvc 30 public class CustomMVCConfiguration implements WebMvcConfigurer { 31 32 @Resource 33 LoginInterceptor loginInterceptor; 34 35 @Autowired 36 LeenledaConfig leenledaConfig; 37 38 @Override 39 public void addInterceptors(InterceptorRegistry registry) { 40 //登錄攔截器 41 registry.addInterceptor(loginInterceptor).addPathPatterns("/admin/**").excludePathPatterns(Arrays.asList("/file/**")); 42 } 43 44 /** 45 * 添加靜態資源文件,外部可以直接訪問地址 46 * 47 * @param registry 48 */ 49 @Override 50 public void addResourceHandlers(ResourceHandlerRegistry registry) { 51 String locationPath = "file:" + leenledaConfig.getLeenledaUploadRoot() + "/leenleda/application/"; 52 registry.addResourceHandler("/file/**") 53 .addResourceLocations(locationPath); 54 } 55 56 @Override 57 public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 58 59 for (int i = converters.size() - 1; i >= 0; i--) { 60 if (converters.get(i) instanceof MappingJackson2HttpMessageConverter) { 61 converters.remove(i); 62 } 63 } 64 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); 65 FastJsonConfig config = new FastJsonConfig(); 66 config.setSerializerFeatures( 67 SerializerFeature.WriteMapNullValue, // 是否輸出值為null的字段,默認為false 68 SerializerFeature.WriteNullListAsEmpty, // 將Collection類型字段的字段空值輸出為[] 69 SerializerFeature.WriteNullStringAsEmpty, // 將字符串類型字段的空值輸出為空字符串 70 SerializerFeature.WriteNullNumberAsZero, // 將數值類型字段的空值輸出為0 71 SerializerFeature.WriteDateUseDateFormat, 72 SerializerFeature.DisableCircularReferenceDetect // 禁用循環引用 73 ); 74 fastJsonHttpMessageConverter.setFastJsonConfig(config); 75 // 添加支持的MediaTypes;不添加時默認為*/*,也就是默認支持全部 76 // 但是MappingJackson2HttpMessageConverter里面支持的MediaTypes為application/json 77 // 參考它的做法, fastjson也只添加application/json的MediaType 78 List<MediaType> fastMediaTypes = new ArrayList<>(); 79 fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); 80 fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes); 81 converters.add(fastJsonHttpMessageConverter); 82 } 83 84 }
