【发布时间】:2017-04-17 23:31:32
【问题描述】:
我有以下 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 响应中,我只看到“内容”。我希望能够将“突出显示”对象添加到 REST 响应中。看起来 HATEOAS 只发送“内容”对象中的信息(对象见下文)。
{
"_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);
}
【问题讨论】:
-
由于
content和highlighted之间似乎有些不同,您应该向我们展示Books的源代码。实际结果的实际 sn-p 和您期望/想要的结果可能会有所帮助。 -
请将变量名和字段名设为小写。大写名称对于 Java 开发人员来说就像类名。
-
如果您发布您的 Books 实体类会有所帮助。
-
@JensSchauder。谢谢。我添加了您建议的信息和一些我认为会有所帮助的其他项目。
-
你是怎么解决这个问题的?
标签: spring rest spring-data spring-data-rest spring-hateoas