【发布时间】:2013-10-13 01:51:00
【问题描述】:
我想编写一个简单的方法来接收代表 POJO 的 json 并使用该 POJO。
示例(接收和返回 POJO):
@Put("json")
public Representation b(JacksonRepresentation<Device> deviceRepresentation)
throws IOException {
Device device = deviceRepresentation.getObject();
// Use the device
return new JacksonRepresentation<Device>(device);
}
上面的例子抛出了一个异常:Conflicting setter definitions for property "locationRef"...
另一种选择是使用 JsonRepresentation,但我找不到将其转换为 POJO 的方法:
@Put("json")
public Representation b(JsonRepresentation jsonRepresentation) {
// How to convert the jsonRepresentation to a POJO???
return new JsonRepresentation(new Device("2", 2));
}
Jackson 听起来是一个更好的工具,因为它具有通用性,因此类型更安全 - 只要它可以工作......
【问题讨论】:
-
这有点像将外部化形式与编译时逻辑混合在一起。我的下意识反应是将 JSON 转换为 Map,然后通过 util 将该 Map 转换为 POJO,然后将该 POJO 传递给您的简单方法。摆脱所有这些注释的东西。 JSON != 对象。