Spring Data Rest:如何在HAL浏览器中公开自定义Rest控制器方法 [英] Spring Data Rest : How to expose custom rest controller method in the HAL Browser

查看:280
本文介绍了Spring Data Rest:如何在HAL浏览器中公开自定义Rest控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义的rest控制器,我可以访问API并从资源中获取结果,问题是,它没有出现在HAL浏览器中.如何在HAL浏览器中公开此自定义方法?谢谢...

i have created a custom rest controller and I can access the API and get the result from the resource, the problem is, it doesn't appear in the HAL Browser.. how to expose this custom method in the HAL Browser? Thank You...

@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浏览器将看到它.

Register a bean that implements a ResourceProcessor<RepositoryLinksResource> and you can add links to your custom controller to the root resource, and the HAL Browser will see it.

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;
    }
}

这篇关于Spring Data Rest:如何在HAL浏览器中公开自定义Rest控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆