Spring boot rest how to provide an end point which has one path variable which takes different types of values
@Autowired
private UserRepository userRepository;
@RequestMapping(value="/user/{idOrName}", method=RequestMethod.GET)
public User findUser(@PathVariable String idOrName) {
this.userRepository.findByIdOrName(idOrName, idOrName);
}
public interface UserRepository extends MongoRepository<User, Long> {
User findByIdOrName(Long id, String name);
}
@Data
@Document(collection="user")
public class User {
private Long Id;
private String name;
}
Problem Statement:
As you can see from the User model, Id is typeof Long
and Name is typeof String
. Now I need to implement single end point which offers facility to search user by id or name.
How can I implement rest end point which is having one path variable which either accept id or name which are of different type?
My problem is Not sure how to define @PathVariable
when user sends either String
or Long
. Is it even possible to do? or should I make @PathVariable
as String
and parse Long
?