Spring Data REST – 使用Java HATEOAS消费实体列表时出现未识别字段”_embedded”。

huangapple 未分类评论49阅读模式
英文:

Spring Data REST - Unrecognized field "_embedded" by consuming list of entities, Java HATEOAS

问题

这是我从您提供的内容中提取并翻译的代码部分:

  1. @Setter
  2. @Getter
  3. @AllArgsConstructor
  4. public class Post {
  5. private int id;
  6. private int branch;
  7. private String article;
  8. private Date datePublished;
  9. private String featuredImage;
  10. private Boolean commentsEnabled;
  11. private Boolean enabled;
  12. private int views;
  13. private String snippetTitle;
  14. private String snippetDescription;
  15. }
  1. ObjectMapper mapper = new ObjectMapper();
  2. MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
  3. messageConverter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
  4. messageConverter.setObjectMapper(mapper);
  5. ResponseEntity<PagedModel<Post>> responseEntity =
  6. new RestTemplate(Arrays.asList(messageConverter)).exchange(uri, HttpMethod.GET, HttpEntity.EMPTY, new ParameterizedTypeReference<PagedModel<Post>>() {});

版本信息:

Jackson-databind 版本:2.11.0

Spring-hateoas 版本:1.0.5.RELEASE

感谢您的帮助!

英文:

I am trying to consume a list of entities from the following REST HAL response:

  1. {
  2. &quot;_embedded&quot; : {
  3. &quot;posts&quot; : [ {
  4. &quot;branch&quot; : 1,
  5. &quot;article&quot; : &quot;aaaaaaa&quot;,
  6. &quot;featuredImage&quot; : &quot;aaaaaaa&quot;,
  7. &quot;authorId&quot; : 1,
  8. &quot;datePublished&quot; : &quot;2020-05-05T09:11:13.336+0000&quot;,
  9. &quot;commentsEnabled&quot; : true,
  10. &quot;enabled&quot; : false,
  11. &quot;views&quot; : 0,
  12. &quot;snippetTitle&quot; : null,
  13. &quot;snippetDescription&quot; : null,
  14. &quot;comments&quot; : null,
  15. &quot;_links&quot; : {
  16. &quot;self&quot; : {
  17. &quot;href&quot; : &quot;http://localhost:8081/posts/1&quot;
  18. },
  19. &quot;post&quot; : {
  20. &quot;href&quot; : &quot;http://localhost:8081/posts/1&quot;
  21. },
  22. &quot;categories&quot; : {
  23. &quot;href&quot; : &quot;http://localhost:8081/posts/1/categories&quot;
  24. }
  25. }
  26. }, {
  27. &quot;branch&quot; : 1,
  28. &quot;article&quot; : &quot;aaaaaaa&quot;,
  29. &quot;featuredImage&quot; : &quot;aaaaaaa&quot;,
  30. &quot;authorId&quot; : 1,
  31. &quot;datePublished&quot; : &quot;2020-05-05T10:45:15.999+0000&quot;,
  32. &quot;commentsEnabled&quot; : true,
  33. &quot;enabled&quot; : false,
  34. &quot;views&quot; : 0,
  35. &quot;snippetTitle&quot; : null,
  36. &quot;snippetDescription&quot; : null,
  37. &quot;comments&quot; : null,
  38. &quot;_links&quot; : {
  39. &quot;self&quot; : {
  40. &quot;href&quot; : &quot;http://localhost:8081/posts/3&quot;
  41. },
  42. &quot;post&quot; : {
  43. &quot;href&quot; : &quot;http://localhost:8081/posts/3&quot;
  44. },
  45. &quot;categories&quot; : {
  46. &quot;href&quot; : &quot;http://localhost:8081/posts/3/categories&quot;
  47. }
  48. }
  49. } ]
  50. },
  51. &quot;_links&quot; : {
  52. &quot;self&quot; : {
  53. &quot;href&quot; : &quot;http://localhost:8081/posts/search/byAuthorId?authorId=1&amp;page=0&amp;size=10&quot;
  54. }
  55. },
  56. &quot;page&quot; : {
  57. &quot;size&quot; : 10,
  58. &quot;totalElements&quot; : 3,
  59. &quot;totalPages&quot; : 1,
  60. &quot;number&quot; : 0
  61. }
  62. }

I would like to map these entities to this class:

  1. @Setter
  2. @Getter
  3. @AllArgsConstructor
  4. public class Post {
  5. private int id;
  6. private int branch;
  7. private String article;
  8. private Date datePublished;
  9. private String featuredImage;
  10. private Boolean commentsEnabled;
  11. private Boolean enabled;
  12. private int views;
  13. private String snippetTitle;
  14. private String snippetDescription;
  15. }

However, I keep getting the error:

> Unrecognized field "_embedded" (class
> org.springframework.hateoas.PagedModel), not marked as ignorable (3
> known properties: "links", "page", "content"])

With this code:

  1. ObjectMapper mapper = new ObjectMapper();
  2. MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
  3. messageConverter.setSupportedMediaTypes(MediaType.parseMediaTypes(&quot;application/hal+json&quot;));
  4. messageConverter.setObjectMapper(mapper);
  5. ResponseEntity&lt;PagedModel&lt;Post&gt;&gt; responseEntity =
  6. new RestTemplate(Arrays.asList(messageConverter)).exchange(uri, HttpMethod.GET, HttpEntity.EMPTY, new ParameterizedTypeReference&lt;PagedModel&lt;Post&gt;&gt;() {});

The versions are:

Jackson-databind version: 2.11.0

Spring-hateoas version: 1.0.5.RELEASE

Any help would be appreciated!

答案1

得分: 0

回复结构看起来类似于 PagedResources<T> 类型。

ParameterizedTypeReference 中使用 org.springframework.hateoas.PagedResources

  1. ResponseEntity<PagedResources<Post>> responseEntity =
  2. new RestTemplate(Arrays.asList(messageConverter)).exchange(uri, HttpMethod.GET, HttpEntity.EMPTY, new ParameterizedTypeReference<PagedResources<Post>>() {});
英文:

Response structure seems like PagedResources&lt;T&gt; type.

Use org.springframework.hateoas.PagedResources in ParameterizedTypeReference

  1. ResponseEntity&lt;PagedResources&lt;Post&gt;&gt; responseEntity =
  2. new RestTemplate(Arrays.asList(messageConverter)).exchange(uri, HttpMethod.GET, HttpEntity.EMPTY, new ParameterizedTypeReference&lt;PagedResources&lt;Post&gt;&gt;() {});

huangapple
  • 本文由 发表于 2020年5月5日 18:58:50
  • 转载请务必保留本文链接:https://java.coder-hub.com/61611533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定