FastJson Fast(快的),还有其他优秀JSON转换器如 jaskson(稳定)、json-lib(出现比较早 资深)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
package com.oo.config;
import java.util.ArrayList;
import java.util.List;
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 com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@Configuration
public class FastJsonMessageConvertersConfig implements WebMvcConfigurer{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(SerializerFeature.PrettyFormat);
//config.setCharset(Charset.forName("UTF-8"));//java.nio.charset.Charset
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);//JSON转换编码
config.setDateFormat("yyyy-MM-dd HH:mm:ss");//时间格式化
converter.setSupportedMediaTypes(fastMediaTypes);
converter.setFastJsonConfig(config);
converters.add(0, converter);
}
}
效果