springboot2.0controller中⽂问号或者乱码的解决办法
开发过程中中⽂⼀直是显⽰,根据⽹上的帖⼦修改配置⽂件后也没有效果。后来偶然间看到⼀篇⽂章,把fastjson改为默认序列化插件,加⼊后就没有问题了。
1package com.leenleda.ward.tv.admin.interceptor;
2
3import com.alibaba.fastjson.serializer.SerializerFeature;
4import com.alibaba.fig.FastJsonConfig;
5import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
6import com.leenleda.fig.LeenledaConfig;
7import org.springframework.beans.factory.annotation.Autowired;
8import t.annotation.Configuration;
9import org.springframework.http.MediaType;
10import org.verter.HttpMessageConverter;
11import org.verter.json.MappingJackson2HttpMessageConverter;
12import org.springframework.fig.annotation.EnableWebMvc;
13import org.springframework.fig.annotation.InterceptorRegistry;
14import org.springframework.fig.annotation.ResourceHandlerRegistry;
15import org.springframework.fig.annotation.WebMvcConfigurer;
springboot中文16
17import javax.annotation.Resource;
18import java.util.ArrayList;
19import java.util.Arrays;
20import java.util.List;
21
22
23/**
24 * @Author: pengbenlei
25 * @Date: 2020/2/19 11:22
26 * @Description:
27*/
28@Configuration
29@EnableWebMvc
30public class CustomMVCConfiguration implements WebMvcConfigurer {
31
32 @Resource
33 LoginInterceptor loginInterceptor;
34
35 @Autowired
36 LeenledaConfig leenledaConfig;
37
38 @Override
39public 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
50public void addResourceHandlers(ResourceHandlerRegistry registry) {
51 String locationPath = "file:" + LeenledaUploadRoot() + "/leenleda/application/";
52 registry.addResourceHandler("/file/**")
53 .addResourceLocations(locationPath);
54 }
55
56 @Override
57public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
58
59for (int i = converters.size() - 1; i >= 0; i--) {
60if ((i) instanceof MappingJackson2HttpMessageConverter) {
61 ve(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 }
WebMvcConfigurer
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论