🚫 不推荐使用 Fastjson,可使用 Jackson 自行封装静态类进行调用,可参考:https://github.com/anydong/example-springboot/blob/main/utils/src/main/java/com/example/demo/utils/JsonUtils.java 实现
pom.xml
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
配置
package com.example.api.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.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* WebMvcConfigurerImpl
*
* @author Where
* @date 2019-01-31
*/
@Configuration
public class WebMvcConfigurerImpl implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteMapNullValue,
SerializerFeature.UseISO8601DateFormat,
SerializerFeature.DisableCircularReferenceDetect
);
config.setCharset(StandardCharsets.UTF_8);
converter.setFastJsonConfig(config);
converters.add(0, converter);
}
}