在 Spring Boot Data Rest 中向 HATEOAS 响应添加更多信息

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

我有以下 REST 控制器。

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

    @Autowired
    public BooksService booksService;

    @Autowired
    private PagedResourcesAssembler<Books> booksAssembler;

    @RequestMapping("/search")
    public HttpEntity<PagedResources<Resource<Books>>> search(@RequestParam(value = "q", required = false) String query, @PageableDefault(page = 0, size = 20) Pageable pageable) {
        pageable = new PageRequest(0, 20);

        Page<Books> booksResult = BooksService.findBookText(query, pageable);

        return new ResponseEntity<PagedResources<Resource<Books>>>(BooksAssembler.toResource(BooksResult), HttpStatus.OK);

    }

我的 Page<Books> BooksResult = BooksService.findBookText(query, pageable); 得到了 SolrCrudRepository 的支持。当它是 运行 时,BookResult 中有几个字段,内容字段和其他几个字段,其中一个是 highlighted。不幸的是,我从 REST 响应中得到的唯一信息是 content 字段中的数据和 HATEOAS 响应中的元数据信息(例如页面信息、链接等)。将 highlighted 字段添加到响应中的正确方法是什么?我假设我需要修改 ResponseEntity,但不确定正确的方法。

编辑:

型号:

@SolrDocument(solrCoreName = "Books_Core")
public class Books {
    @Field
    private String id;

    @Field
    private String filename;

    @Field("full_text")
    private String fullText;

    //Getters and setters omitted 
    ...
}

当搜索和调用 SolrRepository 时(例如 BooksService.findBookText(query, pageable);),我取回了这些对象。

但是,在我的 REST 响应中我只看到 "content"。我希望能够将 "highlighted" 对象添加到 REST 响应中。看起来 HATEOAS 只发送 "content" 对象中的信息(对象见下文)。

{
  "_embedded" : {
    "solrBooks" : [ {
      "filename" : "ABookName",
      "fullText" : "ABook Text"
    } ]
  },
  "_links" : {
    "first" : {
      "href" : "http://localhost:8080/booksCustom/search?q=ABook&page=0&size=20"
    },
    "self" : {
      "href" : "http://localhost:8080/booksCustom/search?q=ABook"
    },
    "next" : {
      "href" : "http://localhost:8080/booksCustom/search?q=ABook&page=0&size=20"
    },
    "last" : {
      "href" : "http://localhost:8080/booksCustom/search?q=ABook&page=0&size=20"
    }
  },
  "page" : {
    "size" : 1,
    "totalElements" : 1,
    "totalPages" : 1,
    "number" : 0
  }
}

为了让您了解全貌,这是支持 BooksService 的存储库。所有服务所做的就是调用这个 SolrCrudRepository 方法。

public interface SolrBooksRepository extends SolrCrudRepository<Books, String> {

    @Highlight(prefix = "<highlight>", postfix = "</highlight>", fragsize = 20, snipplets = 3)
    HighlightPage<SolrTestDocuments> findBookText(@Param("fullText") String fullText, Pageable pageable);

}

我使用 Page<Books> 而不是 HighlightPage 来创建响应页面。页面显然不包含导致突出显示部分被截断的 content。我最终基于 HighlightPage 创建了一个新页面,并将其作为我的结果而不是页面返回。

@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); 
    }

这可能是一种更好的方法,但我找不到任何可以在不更改大量代码的情况下完成我想要它做的事情的方法。希望这对您有所帮助!

好的,我是这样做的: 我写了我的 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;
    }
}

和 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));
    }
}

我必须添加到我的 spring RepositoryRestMvcConfiguration.java:

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

在 cotroller 中,我不得不为新实现的 PagedResourcesAssembler 更改,并在请求方法中使用新的 HighlightPagedResources:

@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);
}

结果:

  {
  "_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"
          ]
        }
      ]
    }
  ]
}