Adding more information to the HATEOAS response in Spring Boot Data Rest

后端 未结 2 1431
忘掉有多难
忘掉有多难 2021-02-10 08:24

I have the following REST controller.

@RepositoryRestController
@RequestMapping(value = \"/booksCustom\")
public class BooksController extends ResourceSupport {
         


        
2条回答
  •  死守一世寂寞
    2021-02-10 08:45

    I was using Page instead of HighlightPage to create the response page. Page obviously doesn't contain content which was causing the highlighted portion to be truncated. I ended up creating a new page based off of HighlightPage and returning that as my result instead of Page.

    @RepositoryRestController
    @RequestMapping(value = "/booksCustom")
    public class BooksController extends ResourceSupport {
    
        @Autowired
        public BooksService booksService;
    
        @Autowired
        private PagedResourcesAssembler booksAssembler;
    
        @RequestMapping("/search")
        public HttpEntity>> search(@RequestParam(value = "q", required = false) String query, @PageableDefault(page = 0, size = 20) Pageable pageable) {
    
            HighlightPage solrBookResult = booksService.findBookText(query, pageable);
            Page highlightedPages = new PageImpl(solrBookResult.getHighlighted(), pageable, solrBookResult.getTotalElements());
            return new ResponseEntity>>(booksAssembler.toResource(highlightedPages), HttpStatus.OK); 
        }
    

    Probably a better way of doing this, but I couldn't find anything that would do what I wanted it to do without having a change a ton of code. Hope this helps!

提交回复
热议问题