首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Data Solr + HATEOAS

Spring Data Solr + HATEOAS
EN

Stack Overflow用户
提问于 2016-03-15 22:45:00
回答 1查看 130关注 0票数 1

在这里玩spring data solr ...我能够返回HATEOAS格式的结果页面,这是很好的,即。

代码语言:javascript
复制
@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:

代码语言:javascript
复制
@RequestMapping("/module/{id}")
Module module(@PathVariable String id) {
    moduleRepository.findOne(id)
}

另外,我如何以HATEOAS形式返回列表?

代码语言:javascript
复制
@RequestMapping("/findAll")
List<Module> findAll() {
    moduleRepository.findAll().content
}
EN

回答 1

Stack Overflow用户

发布于 2016-03-16 16:43:34

实际上,我发现如果我像这样用RepositoryRestResource注释我的存储库,那么我会自动为我公开rest hateaos服务

代码语言:javascript
复制
@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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36014489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档