Spring Data REST - 消耗实体列表时未识别的字段"_embedded",Java HATEOAS。

问题描述 投票:0回答:1

我正试图从以下REST HAL响应中消耗一个实体列表。

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

我想把这些实体映射到这个类上:

@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;
}

然而,我一直收到错误信息:"Unrecognized field "_embedded" (class org.springframework.hateo)

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

用这个代码。

  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。

任何帮助将被感激!

java spring spring-boot jackson spring-hateoas
1个回答
0
投票

响应结构看起来像 PagedResources<T> 型。

使用 org.springframework.hateoas.PagedResourcesParameterizedTypeReference

ResponseEntity<PagedResources<Post>> responseEntity =
    new RestTemplate(Arrays.asList(messageConverter)).exchange(uri, HttpMethod.GET, HttpEntity.EMPTY, new ParameterizedTypeReference<PagedResources<Post>>() {});
© www.soinside.com 2019 - 2024. All rights reserved.