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

后端 未结 2 1432
忘掉有多难
忘掉有多难 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<Books> 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<Books> booksAssembler;
    
        @RequestMapping("/search")
        public HttpEntity<PagedResources<Resource<HighlightPage>>> search(@RequestParam(value = "q", required = false) String query, @PageableDefault(page = 0, size = 20) Pageable pageable) {
    
            HighlightPage solrBookResult = booksService.findBookText(query, pageable);
            Page<Books> highlightedPages = new PageImpl(solrBookResult.getHighlighted(), pageable, solrBookResult.getTotalElements());
            return new ResponseEntity<PagedResources<Resource<HighlightPage>>>(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!

    0 讨论(0)
  • 2021-02-10 08:57

    Ok, here is how I did it: I wrote mine HighlightPagedResources

    public class HighlightPagedResources<R,T> extends PagedResources<R> {
    
        private List<HighlightEntry<T>> phrases;
    
        public HighlightPagedResources(Collection<R> content, PageMetadata metadata, List<HighlightEntry<T>> highlightPhrases, Link... links) {
            super(content, metadata, links);
            this.phrases = highlightPhrases;
        }
    
        @JsonProperty("highlighting")
        public List<HighlightEntry<T>> getHighlightedPhrases() {
            return phrases;
        }
    }
    

    and HighlightPagedResourcesAssembler:

    public class HighlightPagedResourcesAssembler<T> extends PagedResourcesAssembler<T> {
    
        public HighlightPagedResourcesAssembler(HateoasPageableHandlerMethodArgumentResolver resolver, UriComponents baseUri) {
            super(resolver, baseUri);
        }
    
    
        public <R extends ResourceSupport> HighlightPagedResources<R,T> toResource(HighlightPage<T> page, ResourceAssembler<T, R> assembler) {
            final PagedResources<R> rs = super.toResource(page, assembler);
            final Link[] links = new Link[rs.getLinks().size()];
            return new HighlightPagedResources<R, T>(rs.getContent(), rs.getMetadata(), page.getHighlighted(), rs.getLinks().toArray(links));
        }
    }
    

    I had to add to my spring RepositoryRestMvcConfiguration.java:

    @Primary
    @Bean
    public HighlightPagedResourcesAssembler solrPagedResourcesAssembler() {
        return new HighlightPagedResourcesAssembler<Object>(pageableResolver(), null);
    }
    

    In cotroller I had to change PagedResourcesAssembler for newly implemented one and also use new HighlightPagedResources in request method:

    @Autowired
    private HighlightPagedResourcesAssembler<Object> highlightPagedResourcesAssembler;
    
    @RequestMapping(value = "/conversations/search", method = POST)
    
    public HighlightPagedResources<PersistentEntityResource, Object> findAll(
            @RequestBody ConversationSearch search,
            @SortDefault(sort = FIELD_LATEST_SEGMENT_START_DATE_TIME, direction = DESC) Pageable pageable,
            PersistentEntityResourceAssembler assembler) {
    
        HighlightPage page = conversationRepository.findByConversationSearch(search, pageable);
        return highlightPagedResourcesAssembler.toResource(page, assembler);
    }
    

    RESULT:

      {
      "_embedded": {
        "conversations": [
        ..our stuff..
        ]
      },
      "_links": {
        ...as you know them...
      },
      "page": {
        "size": 1,
        "totalElements": 25,
        "totalPages": 25,
        "number": 0
      },
      "highlighting": [
        {
          "entity": {
            "conversationId": "a2127d01-747e-4312-b230-01c63dacac5a",
            ...
          },
          "highlights": [
            {
              "field": {
                "name": "textBody"
              },
              "snipplets": [
                "Additional XXX License for YYY Servers DCL-2016-PO0422 \n   \n<em>hi</em> bodgan  \n    \nwe urgently need the",
                "Additional XXX License for YYY Servers DCL-2016-PO0422\n \n<em>hi</em> bodgan\n \nwe urgently need the permanent"
              ]
            }
          ]
        }
      ]
    }
    
    0 讨论(0)
提交回复
热议问题