springboot 添加拦截器之后中文乱码_springboot2.0 controller中文问号或者乱码的解决办法...

1 packagecom.leenleda.ward.tv.admin.interceptor;2

3 importcom.alibaba.fastjson.serializer.SerializerFeature;4 importcom.alibaba.fastjson.support.config.FastJsonConfig;5 importcom.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;6 importcom.leenleda.ward.tv.common.config.LeenledaConfig;7 importorg.springframework.beans.factory.annotation.Autowired;8 importorg.springframework.context.annotation.Configuration;9 importorg.springframework.http.MediaType;10 importorg.springframework.http.converter.HttpMessageConverter;11 importorg.springframework.http.converter.json.MappingJackson2HttpMessageConverter;12 importorg.springframework.web.servlet.config.annotation.EnableWebMvc;13 importorg.springframework.web.servlet.config.annotation.InterceptorRegistry;14 importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;15 importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;16

17 importjavax.annotation.Resource;18 importjava.util.ArrayList;19 importjava.util.Arrays;20 importjava.util.List;21

22

23 /**

24 * @Author: pengbenlei25 * @Date: 2020/2/19 11:2226 * @Description:27 */

28 @Configuration29 @EnableWebMvc30 public class CustomMVCConfiguration implementsWebMvcConfigurer {31

32 @Resource33 LoginInterceptor loginInterceptor;34

35 @Autowired36 LeenledaConfig leenledaConfig;37

38 @Override39 public voidaddInterceptors(InterceptorRegistry registry) {40 //登录拦截器

41 registry.addInterceptor(loginInterceptor).addPathPatterns("/admin/**").excludePathPatterns(Arrays.asList("/file/**"));42 }43

44 /**

45 * 添加静态资源文件,外部可以直接访问地址46 *47 *@paramregistry48 */

49 @Override50 public voidaddResourceHandlers(ResourceHandlerRegistry registry) {51 String locationPath = "file:" + leenledaConfig.getLeenledaUploadRoot() + "/leenleda/application/";52 registry.addResourceHandler("/file/**")53 .addResourceLocations(locationPath);54 }55

56 @Override57 public void configureMessageConverters(List>converters) {58

59 for (int i = converters.size() - 1; i >= 0; i--) {60 if (converters.get(i) instanceofMappingJackson2HttpMessageConverter) {61 converters.remove(i);62 }63 }64 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = newFastJsonHttpMessageConverter();65 FastJsonConfig config = newFastJsonConfig();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/json77 //参考它的做法, fastjson也只添加application/json的MediaType

78 List fastMediaTypes = new ArrayList<>();79 fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);80 fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);81 converters.add(fastJsonHttpMessageConverter);82 }83

84 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值