SpringBoot+FastJson 配置

package com.zit.config;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.zit.cac.interceptor.LoginInterceptor;

/**
* 

Title: WebMvcConfig

*

Description: 与spring-mvc.xml对应

* @date 2019年4月14日 */ @Configuration @EnableWebMvc public class WebMvcConfig implements WebMvcConfigurer { static Logger logger = LogManager.getLogger(WebMvcConfig.class); @Resource private MappingJackson2HttpMessageConverter converter; @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/WEB-INF/view/", ".jsp"); } @Override public void configureMessageConverters(List> converters) { //1.定义一个convert转换消息对象--字符串解析器 StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8")); //2.将convert添加到converters中并设置优先级 converters.add(0,converter); //1.定义一个convert转换消息对象--json解析器 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //2.1添加fastjson的配置信息 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures( SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteDateUseDateFormat); //2.2处理中文乱码问题 List fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastConverter.setSupportedMediaTypes(fastMediaTypes); //3.在convert中添加配置信息 fastConverter.setFastJsonConfig(fastJsonConfig); //4.将convert添加到converters中并设置优先级 converters.add(1,fastConverter); } @Bean public RequestContextListener requestContextListener(){ return new RequestContextListener(); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()); } }

 

你可能感兴趣的:(SpringCloud)