1、解决方案1(全局,推荐):
List fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fc.setSupportedMediaTypes(fastMediaTypes);
@Configuration
public class MvcConfiger implements WebMvcConfigurer {
/**
* 配置fastjson转换器
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//fastjson
FastJsonHttpMessageConverter fc = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//中文编码格式设置 开始
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fc.setSupportedMediaTypes(fastMediaTypes);
//中文编码格式设置 结束
fc.setFastJsonConfig(fastJsonConfig);
converters.add(fc);
}
}
2、解决方案2(单个):
produces = “application/json;charset=utf-8”
@RequestMapping(
value = "/login",
produces = "application/json;charset=utf-8",
method = RequestMethod.POST
)
本文介绍了两种在SpringBoot中配置FastJSON作为HTTP消息转换器的方法,以支持中文编码和JSON数据格式。解决方案1通过全局配置实现,创建FastJsonHttpMessageConverter并设置FastJsonConfig,确保中文UTF-8编码。解决方案2则是在@RequestMapping注解中指定produces属性,直接在控制器方法级别定义输出内容类型。
784

被折叠的 条评论
为什么被折叠?



