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

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

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

问题

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

@Setter
@Getter
@AllArgsConstructor
public class Post {
  private int id;
  private int branch;
  private String article;
  private Date datePublished;
  private String featuredImage;
  private Boolean commentsEnabled;
  private Boolean enabled;
  private int views;
  private String snippetTitle;
  private String snippetDescription;
}
ObjectMapper mapper = new ObjectMapper();
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();

messageConverter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
messageConverter.setObjectMapper(mapper);

ResponseEntity<PagedModel<Post>> responseEntity =
  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:

    {
  &quot;_embedded&quot; : {
    &quot;posts&quot; : [ {
      &quot;branch&quot; : 1,
      &quot;article&quot; : &quot;aaaaaaa&quot;,
      &quot;featuredImage&quot; : &quot;aaaaaaa&quot;,
      &quot;authorId&quot; : 1,
      &quot;datePublished&quot; : &quot;2020-05-05T09:11:13.336+0000&quot;,
      &quot;commentsEnabled&quot; : true,
      &quot;enabled&quot; : false,
      &quot;views&quot; : 0,
      &quot;snippetTitle&quot; : null,
      &quot;snippetDescription&quot; : null,
      &quot;comments&quot; : null,
      &quot;_links&quot; : {
        &quot;self&quot; : {
          &quot;href&quot; : &quot;http://localhost:8081/posts/1&quot;
        },
        &quot;post&quot; : {
          &quot;href&quot; : &quot;http://localhost:8081/posts/1&quot;
        },
        &quot;categories&quot; : {
          &quot;href&quot; : &quot;http://localhost:8081/posts/1/categories&quot;
        }
      }
    }, {
      &quot;branch&quot; : 1,
      &quot;article&quot; : &quot;aaaaaaa&quot;,
      &quot;featuredImage&quot; : &quot;aaaaaaa&quot;,
      &quot;authorId&quot; : 1,
      &quot;datePublished&quot; : &quot;2020-05-05T10:45:15.999+0000&quot;,
      &quot;commentsEnabled&quot; : true,
      &quot;enabled&quot; : false,
      &quot;views&quot; : 0,
      &quot;snippetTitle&quot; : null,
      &quot;snippetDescription&quot; : null,
      &quot;comments&quot; : null,
      &quot;_links&quot; : {
        &quot;self&quot; : {
          &quot;href&quot; : &quot;http://localhost:8081/posts/3&quot;
        },
        &quot;post&quot; : {
          &quot;href&quot; : &quot;http://localhost:8081/posts/3&quot;
        },
        &quot;categories&quot; : {
          &quot;href&quot; : &quot;http://localhost:8081/posts/3/categories&quot;
        }
      }
    } ]
  },
  &quot;_links&quot; : {
    &quot;self&quot; : {
      &quot;href&quot; : &quot;http://localhost:8081/posts/search/byAuthorId?authorId=1&amp;page=0&amp;size=10&quot;
    }
  },
  &quot;page&quot; : {
    &quot;size&quot; : 10,
    &quot;totalElements&quot; : 3,
    &quot;totalPages&quot; : 1,
    &quot;number&quot; : 0
  }
}

I would like to map these entities to this class:

@Setter
@Getter
@AllArgsConstructor
public class Post {
  private int id;
  private int branch;
  private String article;
  private Date datePublished;
  private String featuredImage;
  private Boolean commentsEnabled;
  private Boolean enabled;
  private int views;
  private String snippetTitle;
  private String snippetDescription;
}

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:

  ObjectMapper mapper = new ObjectMapper();
  MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();

  messageConverter.setSupportedMediaTypes(MediaType.parseMediaTypes(&quot;application/hal+json&quot;));
  messageConverter.setObjectMapper(mapper);

  ResponseEntity&lt;PagedModel&lt;Post&gt;&gt; responseEntity =
    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

ResponseEntity<PagedResources<Post>> responseEntity =
    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

ResponseEntity&lt;PagedResources&lt;Post&gt;&gt; responseEntity =
    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:

确定