Spring Data Rest : How to expose custom rest controller method in the HAL Browser

江枫思渺然 提交于 2019-12-06 03:15:54

问题


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


}

回答1:


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


来源:https://stackoverflow.com/questions/34285829/spring-data-rest-how-to-expose-custom-rest-controller-method-in-the-hal-browse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!