No serializer found for class cn.hutool.json.JSONNull报错
问题:对象的value为null,但将json数据返回前端时key却不显示,使用的是Hutool-json类
@Override
public Object query(BannerDto bannerDto) {
if(StringUtil.isNotEmpty(bannerDto.getSortType()) && "sort".equals(bannerDto.getSortType())){
PageHelper.startPage(bannerDto.getPage(),bannerDto.getPageSize(),"a.sort asc");
}else {
PageHelper.startPage(bannerDto.getPage(),bannerDto.getPageSize(),"a.create_time desc");
}
Page<BannerVo> list = bannerExtendsMapper.selectBySelective(bannerDto);
PageInfo pageInfo = new PageInfo(list,5);
result.setCode(Status.SUCCESS.getIndex());
result.setMsg(Status.SUCCESS.getName());
result.setData(pageInfo);
return JSONUtil.parseObj(result); // 使用的是Hutool-json
}
苦思冥想之下,突然发现Hutool的JSONUtil类提供了**JSONUtil.parseObj()**不忽略空值的方法,顿时“垂死病中惊坐起”,马上动手。
@Override
public Object query(BannerDto bannerDto) {
if(StringUtil.isNotEmpty(bannerDto.getSortType()) && "sort".equals(bannerDto.getSortType())){
PageHelper.startPage(bannerDto.getPage(),bannerDto.getPageSize(),"a.sort asc");
}else {
PageHelper.startPage(bannerDto.getPage(),bannerDto.getPageSize(),"a.create_time desc");
}
Page<BannerVo> list = bannerExtendsMapper.selectBySelective(bannerDto);
PageInfo pageInfo = new PageInfo(list,5);
result.setCode(Status.SUCCESS.getIndex());
result.setMsg(Status.SUCCESS.getName());
result.setData(pageInfo);
return JSONUtil.parseObj(result, false); //添加false参数
}
曙光就在眼前,有点小激动,却没想到反转随时都在,还是报错了,但文章的重头戏也来了。
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: cn.hutool.json.JSONObject["data"]->cn.hutool.json.JSONObject["list"]->cn.hutool.json.JSONArray[0]->cn.hutool.json.JSONObject["title3"])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:313) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:722) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:643) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:33) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:119) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:722) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:643) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:33) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:722) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:643) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:33) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1396) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:913) ~[jackson-databind-2.9.9.3.jar:2.9.9.3]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:287) ~[spring-web-5.1.10.RELEASE.jar:5.1.10.RELEASE]
at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:103) ~[spring-web-5.1.10.RELEASE.jar:5.1.10.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:291)
……
解决方案
创建一个配置类,引入以下这段Bean即可。
package com.dgmall.config;
import cn.hutool.json.JSONNull;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
@Configuration
public class JsonConfig {
@Bean
public ObjectMapper objectMapper() {
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(JSONNull.class, new JsonSerializer<JSONNull>(){
@Override
public void serialize(JSONNull jsonNull, JsonGenerator jsonGenerator
, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeNull();
}
});
simpleModule.addDeserializer(JSONNull.class, new JsonDeserializer<JSONNull>() {
@Override
public JSONNull deserialize(JsonParser jsonParser
, DeserializationContext deserializationContext) {
return null;
}
});
return new ObjectMapper().registerModule(simpleModule);
}
}