相关jar包版本
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.cloud.alibaba.version>2022.0.0.0</spring.cloud.alibaba.version>
<spring.cloud.version>4.0.0</spring.cloud.version>
</properties>
......
<!-- fastjson -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.46</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-extension-spring5</artifactId>
<version>2.0.46</version>
</dependency>
......
FastJsonConfiguration相关代码
@Configuration
public class FastJsonConfiguration implements WebMvcConfigurer {
@Bean
public HttpMessageConverter<?> fastJsonHttpMessageConverter() {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = fastJsonHttpMessageConverter.getFastJsonConfig();
fastJsonConfig.setCharset(StandardCharsets.UTF_8);
fastJsonConfig.setReaderFeatures(
JSONReader.Feature.SupportSmartMatch,
JSONReader.Feature.FieldBased,
JSONReader.Feature.TrimString);
fastJsonConfig.setWriterFeatures(
JSONWriter.Feature.WriteLongAsString,
JSONWriter.Feature.WriteNullListAsEmpty,
JSONWriter.Feature.WriteNullStringAsEmpty,
JSONWriter.Feature.WriteNullBooleanAsFalse,
JSONWriter.Feature.NullAsDefaultValue,
JSONWriter.Feature.PrettyFormat
);
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
fastJsonHttpMessageConverter.setDefaultCharset(StandardCharsets.UTF_8);
List<MediaType> mediaTypeList = new ArrayList<>();
mediaTypeList.add(MediaType.APPLICATION_JSON);
fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypeList);
return fastJsonHttpMessageConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(0, fastJsonHttpMessageConverter());
}
}
Controller返回的泛型对象
.......
public class MyResult<T> {
private final boolean result;
private final String code;
private final String msg;
private final T body;
......
}
目前返回结果中,body为null时
{
"body": null,
"msg": "",
"code": "",
"result": true
}
期望返回
{
"body": {},
"msg": "",
"code": "",
"result": true
}