为什么 PagedResourceAssembler 会清除我的模型链接? [春天 HATEOAS]

时间:2021-01-19 00:10:56

标签: java spring rest spring-hateoas hateoas

我想用 Spring Boot 实现 HATEOAS。我实现了它,现在我有另一个分页问题。我只想说分页是这样工作的:

{
  "links": [
    {
      "rel": "first",
      "href": "http://localhost:8080/api/posts?bookId=1&page=0&size=2"
    },
    {
      "rel": "self",
      "href": "http://localhost:8080/api/posts?bookId=1&page=0&size=2"
    },
    {
      "rel": "next",
      "href": "http://localhost:8080/api/posts?bookId=1&page=1&size=2"
    },
    {
      "rel": "last",
      "href": "http://localhost:8080/api/posts?bookId=1&page=1&size=2"
    }
  ],
  "content": [
    {
      "id": 1,
      "bookId": 1,
      "title": "UpdatedPost1Title",
      "content": "UpdatedPost1Content",
      "created": "2021-01-18T20:42:39",
      "updated": "2021-01-18T23:16:48",
      "voteUp": 0,
      "voteDown": 0,
      "links": [
        
      ]
    },
    {
      "id": 2,
      "bookId": 1,
      "title": "title2",
      "content": "content2",
      "created": "2021-01-18T20:42:43",
      "updated": "2021-01-18T20:42:43",
      "voteUp": 0,
      "voteDown": 0,
      "links": [
        
      ]
    }
  ],

但是正如你所看到的,我所有的内部“链接”都是空的。我运行了调试器,导致这种奇怪行为的行是:

PagedModel<EntityModel<PostDto>> page = pagedResourcesAssembler.toModel(postPage);

从这个方法:

@GetMapping
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<PagedModel<EntityModel<PostDto>>> getPosts(@RequestParam(required = false) Long bookId,
                                                        @PageableDefault(size = DEFAULT_PAGE_SIZE) Pageable pageable) {
        Optional<Long> bookParam = Optional.ofNullable(bookId);

        Page<PostDto> postPage = bookParam.map(aLong -> postService.getByBookId(aLong, pageable).map(assembler::toModel))
                .orElseGet(() -> postService.getAllPosts(pageable).map(assembler::toModel));

        PagedModel<EntityModel<PostDto>> page = pagedResourcesAssembler.toModel(postPage); //this line

        return new ResponseEntity<>(page, HttpStatus.OK);
    }

在此行之前,页面保留有关我之前添加的内部链接的信息 (assembler::toModel)。

我应该怎么做才能防止我的模型被 pagedResourcesAssembler 清除? (它是 org.springframework.data.web.PagedResourcesAssembler)。

先谢谢你!

1 个答案:

答案 0 :(得分:0)

是的,我在这里找到了答案:https://howtodoinjava.com/spring5/hateoas/pagination-links/。它应该是这样的:

@GetMapping
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<PagedModel<PostDto>> getPosts(@RequestParam(required = false) Long bookId,
                                                        @PageableDefault(size = DEFAULT_PAGE_SIZE) Pageable pageable) {
        Optional<Long> bookParam = Optional.ofNullable(bookId);

        Page<Post> postPage = bookParam.map(aLong -> postService.getByBookId(aLong, pageable))
                .orElseGet(() -> postService.getAllPosts(pageable));

        PagedModel<PostDto> page = pagedResourcesAssembler.toModel(postPage, assembler);

        return new ResponseEntity<>(page, HttpStatus.OK);
    }

如您所见,PagedResourcesAssembler 应该是实体类型,而不是模型类型。我还在创建 PagedModel 之前删除了映射并使用 .toModel(Page, RepresentationModelAssembler) 方法。

相关问题