0

I have a Group entity that represents Groups (of users) in a database. The GroupRepository class has some methods that allow me to query the Group table, like, for example: List<Group> findByGroupName(String name);

This method has a URL that's exposed when I curl the URL is the immediate parent of that method, i.e.:

curl http://localhost:8080/groups/search/

{
    "_links" : {
        "searchNames" : {
          "href" : "http://localhost:8080/groups/search/seachNames{?terms}",
          "templated" : true
        },
        "findByName" : {
          "href" : "http://localhost:8080/groups/search/findByName{?name}",
          "templated" : true
    }
}

I am also building a controller to add additional REST endpoints, and I want them to be exposed just like above as "_links". How do make my methods appear like so?

UPDATE
Here is some of my repository code.

@Transactional(readOnly=true)
public interface GroupRepository extends PagingAndSortingRepository<Group, Long> {

    Group findByName(@Param("name") String name);

    @Query(value="SELECT * FROM `Group` WHERE name LIKE CONCAT('%',:terms,'%') ORDER BY CASE WHEN name = :terms THEN 0 WHEN name LIKE CONCAT(:terms,'%') THEN 1 WHEN name LIKE CONCAT('%',:terms,'%') THEN 2 WHEN name LIKE CONCAT('%',:terms) THEN 3 END",
       nativeQuery=true)
    List<Group> searchNames(@Param("terms") String terms);
}
mrks_
  • 359
  • 4
  • 20

1 Answers1

1

This is the HAL representation of a resource. It's a standard.

Here is some theoretical resource : http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

Here is the standard documented: https://datatracker.ietf.org/doc/html/draft-kelly-json-hal-06

If you want to expose data as HAL, have a look at Spring HateOas and Spring Data.

https://spring.io/guides/gs/rest-hateoas/

Community
  • 1
  • 1
okarahan
  • 139
  • 1
  • 14
  • That link to the Spring guide is almost doing I want. Whenever you go to a URL, Spring responds with all child URLs under the "_links" section in the JSON object. I want the methods in my controller, which are mapped to URLs with the @RequestMapping tag to be included in that section. Does that make sense? – mrks_ Apr 30 '15 at 15:29
  • I am not sure if I understand you right. I think it makes sense to represent the controller methods in _links section if the methods represents resources. In your example you have the resource "group" and I would say it is ok to represent this in the hateoas format. – okarahan May 07 '15 at 07:12