I have object User defined as:
@Entity
public class User {
private String name;
}
In my controller, I want to have mapping to query User by name and also query all users. I would like to do something like this:
@GetMapping("/users")
public User getUsers() {...}
@GetMapping("/users")
public User getUserByName(@RequestParam String name) {...}
Producing:
- api/users
- api/users?name=myName
Error is: {GET /api/users}: There is already 'userController' bean method ... mapped
Is there possibility in spring to have GET URL mappings differentiated only by query parameters(@RequestParam) ?
Edit_1: I would like to avoid having to make endpoints for every different getBy variation. So I would like to have only one getBy URI for all different getBy(for name, username, email, address...)
CodePudding user response:
Yes, basically you can configure a request mapping so that it matches if a specific request parameter is present. See https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-requestmapping-params-and-headers for details.
CodePudding user response:
@GetMapping("/users") public User getUserByName(@RequestParam(required = false) String name) {...}