0

I have a very simple example using Spring Data Rest and JPA which exposes a person resources. When launching the application it works as expected and I can POST and GET instances of the resource.

When sending a GET against /person I get the following response:

    "_embedded": {
        "person": [
            {
                "firstName": "FN",
                "lastName": "LN",
                "_links": {
                    "self": {
                        "href": "http://localhost:9090/person/1"
                    },
                    "person": {
                        "href": "http://localhost:9090/person/1"
                    },
                    "address": {
                        "href": "http://localhost:9090/person/1/address"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:9090/person{?page,size,sort}",
            "templated": true
        },
        "profile": {
            "href": "http://localhost:9090/profile/person"
        },
        "search": {
            "href": "http://localhost:9090/person/search"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 1,
        "totalPages": 1,
        "number": 0
    }
}

As you can see the person resource has a firstName attribute with a value of FN.

My question is should the following GET query work out of the box?

/person?firstName=FN

or is this something that needs to be implemented with a custom search method?

Needless to say it isn't working for me but I'm seeing conflicting information as to if it is supported out of the box.

Thanks in advance,

  • You can achieve that with Querydsl, check this answer: https://stackoverflow.com/a/48596145 – Cepr0 Apr 16 '19 at 09:14

1 Answers1

0

My question is should the following GET query work out of the box?

No. You will get an error like

{
"cause": {
"cause": null,
"message": "For input string: \"findByName\""
},
"message": "Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'findByName'; nested exception is java.lang.NumberFormatException: For input string: \"findByName\""
}

Spring Data Rest has the following URL structure. /{pluralEntityName}/{primaryKey} e.g when the entity name is Person and the primary key is Long it will be /persons/1

We see this error message because Spring Date Rest tries to convert the second argument after the entity name to the primary key of that entity. In my Person entity, the primary key is Long, so It tries to convert findByName to Long and fails.

or is this something that needs to be implemented with a custom search method?

Yes, if you want to do a search on the repository you need to write a method in the repository following Spring Data JPA's method name conventions then Spring Data JPA will automatically convert this method to an SQL query and Spring Data Rest will automatically expose this method as an endpoint.

e.g If you write a method like: List findByNameContains(String name);

This method will be exposed with Spring Data Rest and you will be able to access it from the following endpoint: http://localhost:8080/persons/search/findByNameContains?name=Mahsum

Btw, you can see all available search method by visiting http://localhost:8080/persons/search

mahsum
  • 350
  • 3
  • 11