【问题标题】:Spring Data REST - Unrecognized field "_embedded" by consuming list of entities, Java HATEOASSpring Data REST - 通过使用实体列表 Java HATEOAS 无法识别的字段“_embedded”
【发布时间】:2026-01-14 11:10:02
【问题描述】:

我正在尝试使用以下 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;
}

但是,我不断收到错误消息:

无法识别的字段“_embedded”(类 org.springframework.hateoas.PagedModel),未标记为可忽略(3 已知属性:“链接”、“页面”、“内容”])

使用此代码:

  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>>() {});

版本是:

杰克逊数据绑定版本:2.11.0

Spring-hateoas 版本:1.0.5.RELEASE

我们将不胜感激!

【问题讨论】:

    标签: java spring spring-boot jackson spring-hateoas


    【解决方案1】:

    响应结构看起来像PagedResources&lt;T&gt; 类型。

    ParameterizedTypeReference 中使用org.springframework.hateoas.PagedResources

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

    【讨论】:

    • 您好 Eklavya,感谢您的反应,一直很忙,所以没有太多时间来实施您的解决方案。我尝试了您的解决方案,但 PagedResources 已根据此链接重命名为 PagedModel:spring.io/blog/2019/03/05/spring-hateoas-1-0-m1-released
    • 您使用的是哪个 spring-boot 版本?它与 Heteos 兼容吗?目前你不能使用PagedResources 对吧?
    • 这个解决方案确实有效。 PagedResources 已经被 PagedModel 弃用了,我得到了同样的错误