Spring 数据休息:如何在 HAL 浏览器中公开自定义休息控制器方法
Spring Data Rest : How to expose custom rest controller method in the HAL Browser
我已经创建了一个自定义的 rest 控制器,我可以访问 API 并从资源中获取结果,问题是,它没有出现在 HAL 浏览器中..如何公开这个自定义HAL 浏览器中的方法?谢谢...
@RepositoryRestController
public class RevisionController {
protected static final Logger LOG = LoggerFactory
.getLogger(RevisionController.class);
private final DisciplineRepository repository;
Function<Revision<Integer, Discipline>, Discipline> functionDiscipline = new Function<Revision<Integer, Discipline>, Discipline>() {
@Override
public Discipline apply(Revision<Integer, Discipline> input) {
return (Discipline) input.getEntity();
}
};
@Inject
public RevisionController(DisciplineRepository repository) {
this.repository = repository;
}
@RequestMapping(method = RequestMethod.GET, value = "/disciplines/search/{id}/revisions")
public @ResponseBody ResponseEntity<?> getRevisions(
@PathVariable("id") Integer id) {
Revisions<Integer, Discipline> revisions = repository.findRevisions(id);
List<Discipline> disciplines = Lists.transform(revisions.getContent(),
functionDiscipline);
Resources<Discipline> resources = new Resources<Discipline>(disciplines);
resources.add(linkTo(
methodOn(RevisionController.class).getRevisions(id))
.withSelfRel());
return ResponseEntity.ok(resources);
}
}
注册一个实现 ResourceProcessor<RepositoryLinksResource>
的 bean,您可以将自定义控制器的链接添加到根资源,HAL 浏览器会看到它。
public class RootResourceProcessor implements ResourceProcessor<RepositoryLinksResource> {
@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(RevisionController.class).getRevisions(null)).withRel("revisions"));
return resource;
}
}
我已经创建了一个自定义的 rest 控制器,我可以访问 API 并从资源中获取结果,问题是,它没有出现在 HAL 浏览器中..如何公开这个自定义HAL 浏览器中的方法?谢谢...
@RepositoryRestController
public class RevisionController {
protected static final Logger LOG = LoggerFactory
.getLogger(RevisionController.class);
private final DisciplineRepository repository;
Function<Revision<Integer, Discipline>, Discipline> functionDiscipline = new Function<Revision<Integer, Discipline>, Discipline>() {
@Override
public Discipline apply(Revision<Integer, Discipline> input) {
return (Discipline) input.getEntity();
}
};
@Inject
public RevisionController(DisciplineRepository repository) {
this.repository = repository;
}
@RequestMapping(method = RequestMethod.GET, value = "/disciplines/search/{id}/revisions")
public @ResponseBody ResponseEntity<?> getRevisions(
@PathVariable("id") Integer id) {
Revisions<Integer, Discipline> revisions = repository.findRevisions(id);
List<Discipline> disciplines = Lists.transform(revisions.getContent(),
functionDiscipline);
Resources<Discipline> resources = new Resources<Discipline>(disciplines);
resources.add(linkTo(
methodOn(RevisionController.class).getRevisions(id))
.withSelfRel());
return ResponseEntity.ok(resources);
}
}
注册一个实现 ResourceProcessor<RepositoryLinksResource>
的 bean,您可以将自定义控制器的链接添加到根资源,HAL 浏览器会看到它。
public class RootResourceProcessor implements ResourceProcessor<RepositoryLinksResource> {
@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(ControllerLinkBuilder.linkTo(ControllerLinkBuilder.methodOn(RevisionController.class).getRevisions(null)).withRel("revisions"));
return resource;
}
}