首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring Boot Data Rest中将自定义端点公开到base-path

在Spring Boot Data Rest中将自定义端点公开到base-path
EN

Stack Overflow用户
提问于 2019-03-13 00:21:12
回答 1查看 153关注 0票数 1

我正在使用Spring Boot Data Rest,我可以使用下面的url列出所有端点:

代码语言:javascript
复制
http://localhost:8080/api

它列出了以下端点:

代码语言:javascript
复制
{
"_links": {
    "tacos": {
        "href": "http://localhost:8080/api/tacos{?page,size,sort}",
        "templated": true
    },
    "orders": {
        "href": "http://localhost:8080/api/orders"
    },
    "ingredients": {
        "href": "http://localhost:8080/api/ingredients"
    },
    "profile": {
        "href": "http://localhost:8080/api/profile"
    }
}

}

但是我在Controller中创建了一个自定义端点,如下所示

代码语言:javascript
复制
@RepositoryRestController
public class RecentTacosController {

private TacoRepository tacoRepo;

public RecentTacosController(TacoRepository tacoRepo) {
    this.tacoRepo = tacoRepo;
}

@GetMapping(path = "/tacos/recent", produces = "application/hal+json")
public ResponseEntity<Resources<TacoResource>> recentTacos() {

    PageRequest page = PageRequest.of(0, 12, Sort.by("createdAt").descending());
    List<Taco> tacos = tacoRepo.findAll(page).getContent();
    List<TacoResource> tacoResources = new TacoResourceAssembler().toResources(tacos);
    Resources<TacoResource> recentResources = new Resources<TacoResource>(tacoResources);
    recentResources.add(ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(RecentTacosController.class).recentTacos()).withRel("recents"));
    return new ResponseEntity<>(recentResources, HttpStatus.OK);
}

}

但在基本路径上执行GET时不会列出此终结点(http://localhost:8080/api/tacos/recent)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-13 01:29:33

在您的某个类(可以是控制器本身)中实现ResourceProcessor<RepositoryLinksResource>,添加以下方法:

代码语言:javascript
复制
  @Override
  public RepositoryLinksResource process(RepositoryLinksResource resource) {
    resource.add( ... );
    return resource;
  }

..。并在那里创建并添加到您的自定义方法的适当链接。

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

https://stackoverflow.com/questions/55126241

复制
相关文章

相似问题

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