在Spring Boot Data Rest中向HATEOAS响应添加更多信息 [英] Adding more information to the HATEOAS response in Spring Boot Data Rest

查看:180
本文介绍了在Spring Boot Data Rest中向HATEOAS响应添加更多信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下REST控制器.

I have the following REST controller.

@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,但是不确定正确的方法.

My Page<Books> BooksResult = BooksService.findBookText(query, pageable); is backed by SolrCrudRepository. When it is run BookResult has several fields in it, the content field and several other fields, one being highlighted. Unfortunately the only thing I get back from the REST response is the data in the content field and the metadata information in the HATEOAS response (e.g. page information, links, etc.). What would be the proper way of adding the highlighted field to the response? I'm assuming I would need to modify the ResponseEntity, but unsure of the proper way.

修改:

型号:

@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);),我取回了这些对象.

When a search and the SolrRepository is called (e.g. BooksService.findBookText(query, pageable);) I get back these objects.

但是,在我的REST响应中,我仅看到内容".我希望能够将突出显示"对象添加到REST响应中.似乎HATEOAS仅在内容"对象中发送信息(请参阅下面的对象).

However, in my REST response I only see the "content". I would like to be able to add the "highlighted" object to the REST response. It just appears that HATEOAS is only sending the information in the "content" object (see below for the object).

{
  "_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方法.

Just so you can get a full picture, this is the repository that is backing the BooksService. All the service does is call this SolrCrudRepository method.

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的新页面,并将其作为我的结果而不是Page的返回.

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!

这篇关于在Spring Boot Data Rest中向HATEOAS响应添加更多信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆