![](/img/trans.png)
[英]Using Jongo and Jackson 2, how to deserialize a MongoDB ObjectId (represented under String _id in a POJO) to an hexadecimal String representation?
[英]How do I deserialize this string to my POJO representation using Jackson in Java?
public class UpdateRequest {
@JsonProperty
private String id;
@JsonProperty
private String type;
@JsonProperty
private long version;
@JsonProperty("fields")
private Map<String, String> values;
}
我的输入看起来像这样:
{
"id":"1",
"type":"add",
"version": 20146780,
"fields": {
"pid":"1234",
"location":"99,100",
"lastModified":"2013-12-30T22:15:37.482Z"
}
}
这是我目前使用Jackson进行操作的方式,但失败了。 我在这里想念什么?
public Optional<UpdateRequest> deserialize(String message) {
ObjectMapper mapper = new ObjectMapper();
Optional<UpdateRequest> updateRequest = Optional
.empty();
try {
updateRequest = Optional.of(mapper.readValue(message,
UpdateRequest.class));
System.out.println(mapper
.writeValueAsString(updateRequest));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return updateRequest;
}
不确定Optional类在这里做什么,但是您可以通过执行以下操作获取UpdateRequest对象
UpdateRequest updateRequest = mapper.readValue(message, UpdateRequest.class);
您可能想看看这个文档JacksonInFiveMinutes 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.