我有一个来自数据库的结果集Map<String, Object>
,我应该从REST服务返回json
。地图中的值可以是各种类型,包括PGObject
,String
,Integer
和Date
。
我为org.postgresql.util.PGObject
类编写了一个自定义序列化程序,其类型为&#34; jsonb&#34;,它在List<?>
中正常工作,但在Map<String, Object>
中没有。
public class PgObjectSerializer extends JsonSerializer<PGobject>{
@Override
public void serialize(PGobject pgObject, JsonGenerator gen, SerializerProvider serializers) throws IOException {
switch (pgObject.getType()) {
case "json":
case "jsonb":
gen.writeRawValue(pgObject.getValue());
break;
default:
gen.writeString(pgObject.getValue());
}
}
}
目标PGObject如下所示:
PGObject pgo = new PGObject();
pgo.setType("jsonb");
pgo.setValue("[{"id": 6, "name": "Foo"}, {"id": 7, "name": "Bar"}, {"id": 8, "name": "Baz"}]"); map.put("reason", pgo);
当杰克逊在PGObject
中序列化此Map<String, Object>
值时,我会得到json
值,如:
"reason": {
"type": "jsonb",
"value": "[{\"id\": 6, \"name\": \"Foo\"}, {\"id\": 7, \"name\": \"Bar\"}, {\"id\": 8, \"name\": \"Baz\"}]"
}
我需要什么:
"reason": [
{
"id": 6,
"name": "Foo"
},
{
"id": 7,
"name": "Bar"
},
{
"id": 8,
"name": "Baz"
},
],
我已尝试将模块添加到ObjectMapper
,将自定义MapType
添加到ObjectWriter
,如Serializing Map<Date, String> with Jackson的答案所示:
@Service
@Transactional
public class MyClass {
private final ObjectWriter writer;
private final MyRepo repository;
@Autowired
public MyClass(MyRepo repository) {
this.repository = repository;
SimpleModule module = new SimpleModule();
module.addKeySerializer(PGobject.class, new PgObjectSerializer());
ObjectMapper mapper = new ObjectMapper();
JavaType myMapType = mapper.getTypeFactory().
constructMapType(HashMap.class, String.class, PGobject.class);
writer = mapper.registerModule(module).writerFor(myMapType);
}
...
private String toJsonString(Map<String, Object> map) {
try {
return writer.writeValueAsString(map);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
但是我在地图中的每个非PGObject
对象上都会出现序列化错误:
[Test worker] ERROR
com.fasterxml.jackson.databind.JsonMappingException: object is not an instance of declaring class (through reference chain: java.util.HashMap["end_date"]->java.lang.String["type"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:388)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:348)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:343)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:698)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFieldsUsing(MapSerializer.java:736)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:534)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:30)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:416)
at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1425)
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1158)
at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:1031)
如果在序列化PGObjectSerializer
期间找到PGObject作为值时,如何在Jackson中启用Map<String, Object>
?
答案 0 :(得分:0)
可以为Object
添加序列化程序。
然后在序列化程序本身中,您可以检查Object是否为instanceOf PGobject
。
并在myMapType中指定Object.class
:
JavaType myMapType = mapper.getTypeFactory().
constructMapType(HashMap.class, String.class, Object.class);
Serializer将是:
class PgObjectSerializer extends JsonSerializer<Object> {
@Override
public void serialize(Object object, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (object instanceof PGobject) {
PGobject pgObject = (PGobject) object;
switch (pgObject.getType()) {
case "json":
case "jsonb":
gen.writeRawValue(pgObject.getType());
break;
default:
gen.writeString(pgObject.getType());
}
}else{
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(gen, object);
}
}
}