在这里玩spring data solr ...我能够返回HATEOAS格式的结果页面,这是很好的,即。
@RequestMapping("/findAllPaged")
HttpEntity<PagedResources<Module>> findAllPaged(Pageable pageable, PagedResourcesAssembler assembler) {
Page<Module> page = moduleRepository.findAll(pageable)
return new ResponseEntity<>(assembler.toResource(page), HttpStatus.OK);
}但是,如何让方法以正确的HATEOAS格式返回一个实体呢
我目前做了以下工作,这为我提供了基本的json序列化,但不确定如何获得HATEOAS:
@RequestMapping("/module/{id}")
Module module(@PathVariable String id) {
moduleRepository.findOne(id)
}另外,我如何以HATEOAS形式返回列表?
@RequestMapping("/findAll")
List<Module> findAll() {
moduleRepository.findAll().content
}发布于 2016-03-16 16:43:34
实际上,我发现如果我像这样用RepositoryRestResource注释我的存储库,那么我会自动为我公开rest hateaos服务
@RepositoryRestResource
interface ModuleRepository extends SolrCrudRepository<Module, String>{
/**
* Does exact (equals) query
* @param name
* @param description
* @param pageable
* @return
*/
Page<Module> findByNameOrDescription(@Param(value = "name")@Boost(2.0F) String name, @Param(value = "description") String description, Pageable pageable);
/**
* Does like query and ignores case
* @param name
* @param description
* @param pageable
* @return
*/
Page<Module> findByNameContainingOrDescriptionContainingAllIgnoreCase(@Param(value = "name")String name, @Param(value = "description") String description, Pageable pageable);
/**
* Custom query example
* @param name
* @param pageable
* @return
*/
@Query(value = "name:*?0*")
Page<Module> myCustomQuery(@Param(value = "name")String name,Pageable pageable);
@Query(value = "name:*?0* OR description:*?0* ")
Page<Module> myOtherCustomQuery(@Param(value = "name")String name,Pageable pageable);
Page<Module> findByNameContainingIgnoreCase(@Param(value = "name")String name, Pageable pageable);
}例如:
http://localhost:8080/modules/所有文档
http://localhost:8080/modules/1 one文档
http://localhost:8080/modules?page=0&size=4&sort=name,desc分页结果http://localhost:8080/modules/search/findByNameOrDescription?name=name1&description=description2&page=0&size=10
http://localhost:8080/modules/search/findByNameContainingOrDescriptionContainingAllIgnoreCase?name=mAt&description=sCi&page=0&size=10
https://stackoverflow.com/questions/36014489
复制相似问题