【发布时间】:2020-07-05 11:52:57
【问题描述】:
我想序列化一些字符串json(我用的是Spring Boot):
{
"commandId": "34d3a914-a3d7-112a-bs37-3452ac130002",
"status": "SUCCESS",
"response": {
"prop1": "",
"prop2": "",
"prop3": "true",
"prop4": ""
}
}
我的映射器:
private static final ObjectMapper mapper = new ObjectMapper();
public static <T> T fromJson(String json, TypeReference<T> type) {
T t;
try {
t = mapper.reader().forType(type).readValue(json);
} catch (IOException e) {
String message = "Error converting from json";
log.error(message, e);
throw new IllegalArgumentException(message);
}
mapper的用法:
final Command command =
JsonUtils.fromJson(json, new TypeReference<Command>() {});
命令类:
public class Command {
private UUID commandId;
private status status;
private String response;
private String error;
}
目前我收到错误:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
我想在 Mongo db response 字段中保存为字符串,稍后在从 DB 接收到该对象后,我可以反序列化整个对象。是否有任何注释告诉 Json 映射器不要序列化 response 字段内的结构?
【问题讨论】:
标签: java json spring-boot json-deserialization json-serialization