我有我的杰克逊对象:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
@Id @ObjectId
private String _id;
private String firstName;
private String lastName;
public Address address;
//getters / Setters
}
当我运行单元测试以使用默认的ObjectMapper将json String转换为Jackson对象时,我可以看到地址被正确转换! =>测试通过
@Test
public void UserWithAddressShouldGiveValidJSONObject () throws Exception {
//Given
String userJSON = "{\"firstName\":\"Michel\",\"lastName\":\"Thom\",\"address\":{\"no\":\"12\",\"street\":\"example street\",\"postalCode\":\"555\",\"city\":\"New York\"}}";
//When
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(userJSON, User.class);
//Then
assertEquals("Michel", user.getFirstName());
assertEquals("Thom", user.getLastName());
assertEquals("New York", user.getAddress().getCity());
}
但是当我在泽西岛获得相同的json时,Address对象是空的!
@POST
@Consumes("application/json")
public Response saveUser(User user) throws Exception {
user.toString();
}
泽西收到了杰森:
{ "firstName" : "Michel", "lastName" : "Thom", "address" : { }}
我缺少什么?
答案 0 :(得分:1)
您的web.xml文件中是否有此声明?
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
我在我的泽西项目中使用此功能并且没有遇到过您的问题。我读到它有助于使用嵌套的JSONS(和返回)解析来自JSON的对象。
希望这有帮助。