<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
import java.io.IOException;
import java.nio.charset.Charset;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
public class MJFastJsonHttpMessageConverter extends FastJsonHttpMessageConverter {
public static final Charset UTF8 = Charset.forName("UTF-8");
private Charset charset;
private SerializerFeature[] features;
public MJFastJsonHttpMessageConverter() {
super();
this.charset = UTF8;
this.features = new SerializerFeature[0];
}
@Override
protected void writeInternal(Object obj, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
// obj就是controller中注解为@ResponseBody的方法返回值对象
if (obj instanceof ApiResult) {
String text = JSONObject.toJSONString(obj, this.features);
byte[] bytes = text.getBytes(this.charset);
outputMessage.getBody().write(bytes);
} else {
String text = JSONObject.toJSONString(ApiResult.valueOf(obj), this.features);
byte[] bytes = text.getBytes(UTF8);
outputMessage.getBody().write(bytes);
}
}
}
import java.util.List;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
/**
WriteNullListAsEmpty :List字段如果为null,输出为[],而非null
WriteNullStringAsEmpty : 字符类型字段如果为null,输出为"",而非null
DisableCircularReferenceDetect :消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
WriteNullBooleanAsFalse:Boolean字段如果为null,输出为false,而非null
WriteMapNullValue:是否输出值为null的字段,默认为false
* @author admin
*
*/
@Configuration
public class MyWebmvcConfiguration implements WebMvcConfigurer{
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
/**
* 1.先定义一个convert转换消息的对象
* 2.添加fastjson的配置信息,比如:是否要格式化返回的json数据
* 3.在convert中添加配置信息
* 4.将convert添加到converters当中
*/
//1.先定义一个convert转换消息的对象
MJFastJsonHttpMessageConverter fastConverter = new MJFastJsonHttpMessageConverter();
return new HttpMessageConverters(fastConverter);
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
MJFastJsonHttpMessageConverter fjc = new MJFastJsonHttpMessageConverter();
FastJsonConfig fj = new FastJsonConfig();
fj.setSerializerFeatures(
SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteNullStringAsEmpty,
// Number null -> 0
SerializerFeature.WriteNullNumberAsZero
);
fjc.setFastJsonConfig(fj);
converters.add(fjc);
for (HttpMessageConverter<?> messageConverter : converters) {
System.out.println(messageConverter); //2
}
}
}