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

后端 未结 2 1437
忘掉有多难
忘掉有多难 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:57

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

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

    and HighlightPagedResourcesAssembler:

    public class HighlightPagedResourcesAssembler extends PagedResourcesAssembler {
    
        public HighlightPagedResourcesAssembler(HateoasPageableHandlerMethodArgumentResolver resolver, UriComponents baseUri) {
            super(resolver, baseUri);
        }
    
    
        public  HighlightPagedResources toResource(HighlightPage page, ResourceAssembler assembler) {
            final PagedResources rs = super.toResource(page, assembler);
            final Link[] links = new Link[rs.getLinks().size()];
            return new HighlightPagedResources(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(pageableResolver(), null);
    }
    
    
    

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

    @Autowired
    private HighlightPagedResourcesAssembler highlightPagedResourcesAssembler;
    
    @RequestMapping(value = "/conversations/search", method = POST)
    
    public HighlightPagedResources 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   \nhi bodgan  \n    \nwe urgently need the",
                "Additional XXX License for YYY Servers DCL-2016-PO0422\n \nhi bodgan\n \nwe urgently need the permanent"
              ]
            }
          ]
        }
      ]
    }
    

    提交回复
    热议问题