为什么pagedresourceassembler清除我的模型链接[[春夏]

qaxu7uf2  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(257)

我想用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)。
提前谢谢!

xuo3flqw

xuo3flqw1#

是的,我在这里找到了答案: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)方法之前,我还移除了Map。

相关问题