Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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


}
like image 890
Azli Cn Avatar asked Sep 25 '22 22:09

Azli Cn


1 Answers

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;
    }
}
like image 153
adam p Avatar answered Oct 12 '22 11:10

adam p