项目中没有使用fastjson解析字符串, 原因是fastjson经常爆出漏洞, 服务器安全监测不通过.
项目中使用hutool工具类操作字符串,在@ResponseBody序列化时结果值为 null 会报异常
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class cn.hutool.json.JSONNull]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class cn.hutool.json.JSONNull and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.tgpms.utils.Result["data"]->cn.hutool.json.JSONObject["avatar"])
解决办法: 注册一个objectMapper覆盖掉默认的
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
解决方法二
不使用SpringBoot默认的jackson进行对象json化,手动使用其他json框架如fastJSON进行json化然后返回。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>x.x.x</version>
</dependency>