5

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 Answer 1

4

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;
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

I'm trying that, and it's failing for me with java.lang.IllegalArgumentException: 'uriTemplate' must not be null. (Your code seemed to be missing a close-paren after getRevisions(). )
Thanks Eric, I've amended the code. As for your issue - could you post a more complete stack trace? It is possible that you will be able to fix this by having your custom controller class implement the ResourceProcessor interface rather than a standalone class - couple of rumblings about this elsewhere (stackoverflow.com/questions/38548834/…).
Well, now I'm getting java.util.NoSuchElementException: ArrayList:854 Collections:1042 ControllerLinkBuilderFactory:139 [spring-hateoas] ControllerLinkBuilder:172 RootResourceProcessor:22) which has resource.add(linkTo(methodOn(MeetupController.class, 2L, 3L, 2d, 2d, null, null).finalize(2L, 3L, 2d, 2d, null, null)).withRel("meetups")); (Stack truncated to fit in comment)
Without being able to see your controller code this is tricky to debug further. I'm not sure why you are passing those parameters to your method twice. Can you not do this? resource.add(linkTo(methodOn(MeetupController.class).finalize(2L, 3L, 2d, 2d, null, null)).withRel("meetups"));
(I had passed params twice because of a problem I thought that was fixing). Yes - adjusting it again (and apparently fixing whatever that other problem was) makes the method exposed on the HAL browser. But, (1) at the top level instead of where I'd like, i.e /meetups/finalize, (2) the params are hard-coded to those above, rather than templated, and (3) I'd like it to be available only as a POST (non-GET)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.