EntityModel的春季Hateoas错误:内容不是地图

发布于 2025-02-03 13:37:45 字数 3410 浏览 3 评论 0 原文

我正在从 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

I am upgrading spring-hateoas from 0.20.0.RELEASE to 1.3.7

I replaced ResourceSupport with RepresentationModel and Resource with RepresentationModel.

While deserialising the JSON of the DTO I am getting an exception, which is working fine in the old version.

DTO with old Hateoas version

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 with new Hateoas version

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 file to be deserialised employee.json

{
  "employees": [
    {
      "id": 1,
      "name": "Test",
      "links": [
        {
          "rel": "self",
          "href": "api/employees/1"
        }
      ]
    }
  ]
}

Test case of deserialisation

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咽泪装欢 2025-02-10 13:37:46

super(code> super() EntityModel 的构造函数中删除 new Employee() 解决了问题,但不确定原因。

public class EmployeeResource extends Resource<Employee> {
    EmployeeResource(){
        super();
    }
}

Removing new Employee() from super() constructor of EntityModel resolved the issue, but not sure the reason.

public class EmployeeResource extends Resource<Employee> {
    EmployeeResource(){
        super();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文