已经废弃的方法:
SpringBoot2.0开始,采用继承WebMvcConfigurerAdapter的方式已经过时,请不要再继续使用。
maven导入fastjson的坐标,不再啰嗦。
所有maven坐标都可以去这里找到。Maven仓库
两种方式:
第一种:创建自定义消息转换器配置类,通过@Bean注入替换默认的消息转换器
第二种:实现WebMvcConfigurer,重写configureMessageConverters方法
第三种:继承WebMvcConfigurationSupport,不再介绍
上代码:
第一种:
package com.ieslab.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* @version V1.0
* @Title:
* @Package com.example.demo
* @Description: TODO
* @author: zongshaofeng
* @date 2021/6/12
*/
@Configuration
public class MessageConverterConfig {
/**
* 配置消息转换器
* new HttpMessageConverters(true, converters);
* 一定要设为true才能替换否则不会替换
* @return 返回一个消息转换的bean
*/
@Bean
public HttpMessageConverters fastJsonMessageConverters() {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
//需要定义一个convert转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//添加fastJson的配置信息;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
//关闭循环引用
fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
//全局时间配置
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
fastJsonConfig.setCharset(Charset.forName("UTF-8"));
//处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
//在convert中添加配置信息.
fastConverter.setSupportedMediaTypes(fastMediaTypes);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(0, fastConverter);
return new HttpMessageConverters(true,converters);
}
}
第二种:
package com.ieslab.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* @version V1.0
* @Title: WebMvcConfig类
* @Package com.ieslab.config
* @Description: TODO
* @author: zongshaofeng
* @date 2021/6/12
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
/**
* 全局配置跨域问题解决方案
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")//允许跨域的域名,可以用*表示允许任何域名使用
.allowedMethods("*") //允许任何方法(post、get等)
.allowedHeaders("*") //允许任何请求头
.allowCredentials(true) //带上cookie信息
//maxAge(3600)表明在3600秒内,不需要再发送预检验请求,可以缓存该结果
.exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L);
}
/**
* 配置消息转换器
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//需要定义一个convert转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//添加fastJson的配置信息;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
//关闭循环引用
fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
//全局时间配置
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
fastJsonConfig.setCharset(Charset.forName("UTF-8"));
//处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
//在convert中添加配置信息.
fastConverter.setSupportedMediaTypes(fastMediaTypes);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(0, fastConverter);
}
}