Spring Data Rest:如何在HAL浏览器中公开自定义rest控制器方法

时间:2015-12-15 09:48:19

标签: spring spring-data-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);
    }


}

1 个答案:

答案 0 :(得分:4)

注册实现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;
    }
}