In springboot you can have a repository that extends JpaRepository<User, String>
Now if you want to write a query to that repository, you can write multiple findBy queries such as:
User findByUsername(String username);
User findByFirstName(String firstName);
User findByFirstNameAndLastName(String firstName, String lastName);
then you need matching controllers like:
@RequestMapping(value = "/users/{username}" , method = RequestMethod.GET)
public @ResponseBody List<User> getUser(@PathVariable("username") String username) {
return userService.findByUsername(username);
}
etc, etc...
I don't want to have to write every single findBy for each combination of elements and create all the matching controllers like that.
So what I want to know is how you can search by a whole object. For example posting something like
{
"username":"freddy"
}
will search by username and equally
{
"firstName":"Fred",
"lastName":"Doe"
}
will search by first and last name.
Is there a way to write both a JpaRepository method and a controller that can take a (in this case) User object with any combination of fields, and perform a search for that?
Thanks.