Spring Data Solr + HATEOAS

时间:2016-03-15 14:45:00

标签: spring-hateoas spring-data-solr

在这里玩弹簧数据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形式返回List?

@RequestMapping("/findAll")
List<Module> findAll() {
    moduleRepository.findAll().content
}

1 个答案:

答案 0 :(得分:0)

实际上我发现如果我使用RepositoryRestResource注释我的存储库,那么我会自动为我公开休息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);



}

e.g。

http://localhost:8080/modules/所有文档

http://localhost:8080/modules/1一个文档

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