EntityModel的春季Hateoas错误:内容不是地图
我正在从 0.20.0.Release
1.3.7
i替换 i替换 resourcesupport
用 spring-hateoas
1.3.7 >表示模特和资源
带有表示模特
。
当dto的 json
时,我会得到一个例外,这在旧版本中正常工作。
带有旧Hateoas版本DTO的
public class Employee extends ResourceSupport implements Serializable {
@JsonProperty("id")
private Long id;
@JsonProperty("name")
private String name;
}
public class EmployeeResource extends Resource<Employee> {
EmployeeResource(){
super(new Employee());
}
}
DTO带有新的Hateoas版本
public class Employee extends RepresentationModel<Employee> implements Serializable {
@JsonProperty("id")
private Long id;
@JsonProperty("name")
private String name;
}
public class EmployeeResource extends EntityModel<Employee> {
EmployeeResource(){
super(new Employee());
}
}
JSON
deleialialise 雇员
{
"employees": [
{
"id": 1,
"name": "Test",
"links": [
{
"rel": "self",
"href": "api/employees/1"
}
]
}
]
}
测试案例deerialisation
@Test
public void test() throws IOException {
ObjectMapper mapper = new ObjectMapper();
InputStream inputStream = EmpTest.class.getClassLoader().getResourceAsStream("employee.json");
JsonNode node = mapper.readTree(IOUtils.toByteArray(inputStream));
TypeFactory typeFactory = mapper.getTypeFactory();
CollectionType listType = typeFactory.constructCollectionType(ArrayList.class, EmployeeResource.class);
List resultList = mapper.convertValue(node.findValue("employees"), listType);
System.out.println(resultList);
}
Exception
java.lang.IllegalArgumentException: Content is not a Map! (through reference chain: java.util.ArrayList[0]->com.test.EmployeeResource["id"])
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3589)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3530)
at com.test.EmpTest.test(EmpTest.java:60)
Caused by: java.lang.IllegalStateException: Content is not a Map!
at org.springframework.util.Assert.state(Assert.java:70)
at org.springframework.hateoas.EntityModel.getOrInitAsMap(EntityModel.java:158)
at org.springframework.hateoas.EntityModel.setPropertiesAsMap(EntityModel.java:149)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.fasterxml.jackson.databind.introspect.AnnotatedMethod.callOnWith(AnnotatedMethod.java:130)
at com.fasterxml.jackson.databind.deser.SettableAnyProperty.set(SettableAnyProperty.java:172)
... 33 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从
super(code> super()
EntityModel
的构造函数中删除new Employee()
解决了问题,但不确定原因。Removing
new Employee()
fromsuper()
constructor ofEntityModel
resolved the issue, but not sure the reason.