| New file | 
|  |  |  | 
|---|
|  |  |  | package com.dy.common.fastjson; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSONWriter; | 
|---|
|  |  |  | import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter; | 
|---|
|  |  |  | import org.springframework.context.annotation.Configuration; | 
|---|
|  |  |  | import org.springframework.http.MediaType; | 
|---|
|  |  |  | import org.springframework.http.converter.HttpMessageConverter; | 
|---|
|  |  |  | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.nio.charset.StandardCharsets; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 必须实现WebMvcConfigurer否则不启作用,即Controller转json时不用fastjson | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Configuration | 
|---|
|  |  |  | public class FastJsonConfig implements WebMvcConfigurer { | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 配置fastjson输出格式 | 
|---|
|  |  |  | **/ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { | 
|---|
|  |  |  | // 1. 配置fastjson | 
|---|
|  |  |  | com.alibaba.fastjson2.support.config.FastJsonConfig config = new com.alibaba.fastjson2.support.config.FastJsonConfig(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | config.setDateFormat("yyyy-MM-dd HH:mm:ss"); | 
|---|
|  |  |  | config.setCharset(StandardCharsets.UTF_8); | 
|---|
|  |  |  | config.setWriterFeatures( | 
|---|
|  |  |  | //输出map中value为null的数据 | 
|---|
|  |  |  | JSONWriter.Feature.WriteMapNullValue, | 
|---|
|  |  |  | //输出null的boolean 为 false | 
|---|
|  |  |  | JSONWriter.Feature.WriteNullBooleanAsFalse, | 
|---|
|  |  |  | //把Long型输出为String,在pojo的属性中个性设置 | 
|---|
|  |  |  | //JSONWriter.Feature.WriteLongAsString, | 
|---|
|  |  |  | //输出null的list 为 [] | 
|---|
|  |  |  | JSONWriter.Feature.WriteNullListAsEmpty, | 
|---|
|  |  |  | //输出null的number 为 0 | 
|---|
|  |  |  | //JSONWriter.Feature.WriteNullNumberAsZero, | 
|---|
|  |  |  | //输出null字符串 为 "" | 
|---|
|  |  |  | JSONWriter.Feature.WriteNullStringAsEmpty, | 
|---|
|  |  |  | //对map进行排序 | 
|---|
|  |  |  | JSONWriter.Feature.MapSortField, | 
|---|
|  |  |  | //json格式化 | 
|---|
|  |  |  | JSONWriter.Feature.PrettyFormat | 
|---|
|  |  |  | ); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 2. 添加fastjson转换器 | 
|---|
|  |  |  | FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); | 
|---|
|  |  |  | converter.setDefaultCharset(StandardCharsets.UTF_8); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 3. 添加支持的媒体类型 | 
|---|
|  |  |  | List<MediaType> supportedMediaTypes = new ArrayList<>(); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.APPLICATION_JSON); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.APPLICATION_PDF); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.APPLICATION_XML); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.IMAGE_GIF); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.IMAGE_JPEG); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.IMAGE_PNG); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.TEXT_HTML); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.TEXT_MARKDOWN); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.TEXT_PLAIN); | 
|---|
|  |  |  | supportedMediaTypes.add(MediaType.TEXT_XML); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | converter.setSupportedMediaTypes(supportedMediaTypes); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //4 将convert添加到converters | 
|---|
|  |  |  | converter.setFastJsonConfig(config); | 
|---|
|  |  |  | converters.add(0,converter); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|