3
votes

I would like to combine HATEAOS links to methods on both Controller and Repository.

@RepositoryRestController
@ResponseBody
@ExposesResourceFor(Group.class)
@RequestMapping(value = "/api/v2/groups", produces = MediaTypes.HAL_JSON_VALUE)
public class GroupController {

    @Resource
    private GroupService groupService;

    @RequestMapping(value = "/external", method = POST)
    public  @ResponseBody   PersistentEntityResource saveExternalGroup(
            @RequestBody Group newGroup,
            PersistentEntityResourceAssembler assembler) {

        return assembler.toResource(groupService.saveExternalGroup(newGroup));

    }

}

Repository:

@RepositoryRestResource(excerptProjection = GroupSummary.class)
public interface GroupDao extends DefaultDao<Group, Long> {

    @NotNull
    List<Group> findByState(@Nullable GroupState state);
...other methods...

I would like to achieve to have possibility to go to /api/v2/groups and have there also link to /external. Currently, only links from repository are returned:

"_links": {
    "first": {
      "href": "http://localhost:8300/api/v2/groups?page=0&size=20"
    },
    "self": {
      "href": "http://localhost:8300/api/v2/groups"
    },
    "next": {
      "href": "http://localhost:8300/api/v2/groups?page=1&size=20"
    },
    "last": {
      "href": "http://localhost:8300/api/v2/groups?page=1&size=20"
    },
    "profile": {
      "href": "http://localhost:8300/api/v2/profile/groups"
    },
    "search": {
      "href": "http://localhost:8300/api/v2/groups/search"
    }
  },

What should I implement to get there all as above plus something like this:

"external": {
          "href": "http://localhost:8300/api/v2/groups/external"
        }

Or is there problem with that "/external" is POST? If so, please comment and consider this question with "method=GET".

1

1 Answers

0
votes

Option 1:

If it's a one-off, you could add the links in the controller method using the Resource class.

@RequestMapping(value = "/external", method = POST)
public  @ResponseBody   PersistentEntityResource saveExternalGroup(
        @RequestBody Group newGroup,
        PersistentEntityResourceAssembler assembler) {

    PersistentEntityResource resource = assembler.toResource(groupService.saveExternalGroup(newGroup));

    // Replace with ControllerLinkBuilder call, or EntityLinks as you see fit.
    resource.add(new Link("http://localhost:8300/api/v2/groups/external","search"));

    return resource;
}

Option 2:

If you would like this link to be added to every rendered Resource<Group>, then create a ResourceProcessor component to add it.

@Component
public class GroupResourceProcessor implements ResourceProcessor<Resource<Group>> {

    @Override
    public Resource<Group> process(Resource<Group> groupResource) {

        // Replace with ControllerLinkBuilder call, or EntityLinks as you see fit.
        groupResource.add(new Link("http://localhost:8300/api/v2/groups/external","search"));

        return groupResource;
    }

}