在 Spring Boot Data Rest 中将自定义端点公开到基本路径
Expose custom endpoint to base-path in Spring Boot Data Rest
我正在使用 Spring Boot Data Rest,我可以使用 follling url:
列出所有端点
http://localhost:8080/api
它列出了以下端点:
{
"_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"
}
}
}
但我在控制器中创建了一个自定义端点,如下所示
@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)。
在您的 class 之一(可能是控制器本身)中实现 ResourceProcessor<RepositoryLinksResource>
,添加以下方法:
@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add( ... );
return resource;
}
... 并创建适当的 link 并将其添加到您的自定义方法中。
我正在使用 Spring Boot Data Rest,我可以使用 follling url:
列出所有端点http://localhost:8080/api
它列出了以下端点:
{
"_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"
}
}
}
但我在控制器中创建了一个自定义端点,如下所示
@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)。
在您的 class 之一(可能是控制器本身)中实现 ResourceProcessor<RepositoryLinksResource>
,添加以下方法:
@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add( ... );
return resource;
}
... 并创建适当的 link 并将其添加到您的自定义方法中。