123

I have scenario where one url "serachUser" may come with two different value (request parameter) userId or UserName.

so for this I have created two methods

public String searchUserById(@RequestParam long userID, Model model) 
public ModelAndView searchUserByName(@RequestParam String userName)

But i am getting Ambiguous mapping found exception. Can Spring handle this situation?

Guerric P
  • 30,447
  • 6
  • 48
  • 86
Vikas Singh
  • 2,838
  • 5
  • 17
  • 32

2 Answers2

248

You can use the params parameter to filter by HTTP parameters. In your case it would be something like:

@RequestMapping(value = "/searchUser", params = "userID")
public String searchUserById(@RequestParam long userID, Model model) {
  // ...
}

@RequestMapping(value = "/searchUser", params = "userName")
public ModelAndView searchUserByName(@RequestParam String userName) {
  // ...
}
kryger
  • 12,906
  • 8
  • 44
  • 65
  • The other way I had handled this is to accept the parameter as a String, then call Long.parseLong() on it. If it parses, then its the the userId, if it doesn't, assume its the username. – CodeChimp Apr 08 '13 at 19:42
  • 17
    In case you want to know how it works when you have multiple params, you can use `params = { "storeId", "containerLabel" }` – cindyxiaoxiaoli Jan 07 '16 at 16:50
  • if url mapping and all other parameters are same, is it give a deployment time exception? – Janath Apr 08 '16 at 06:48
  • But will it be possible to assign different role/function to this url to authorise? – Kanagavelu Sugumar Nov 21 '17 at 15:30
  • 3
    Related: Swagger might not fully support this. I found this issue http://github.com/springfox/springfox/issues/1828. I use Spring Fox 2.7.0 and the Swagger UI only shows 1 method instead of 2. Apparently this can be fixed by `enableUrlTemplating(true)` and using an experimental Swagger UI: http://springfox.github.io/springfox/docs/current/#springfox-rfc6570-support-strong-incubating-strong – Stephanie Apr 30 '18 at 11:58
  • The URL should have that userID keyword in it. Spring matched it using that. – AnirbanDebnath Jan 11 '19 at 10:42
  • what if I call `/searchUser?userID=abc&userName=xyz`? Which of the controller will get called? – Obaid Maroof Jul 08 '19 at 07:09
  • What if the incoming request has a JSON body, which can be mapped to two types of DTO? Does Spring have some mechanism to distinguish them? – WesternGun Oct 08 '19 at 14:51
  • @WesternGun I'm also interested in this. Have you found something? – Quinten Scheppermans Jan 02 '20 at 14:38
  • @QuintenScheppermans sorry, not yet. Till this moment, I think I have to pass an extra param to distinguish them. Cannot see any other way around. – WesternGun Jan 02 '20 at 21:59
0

Any way incase of request param null is allowed if you don't pass any value it will be null then you can write your coad like:

@RequestMapping(value = "/searchUser", params = {"userID","userName"})
public String searchUserById(@RequestParam long userID,@RequestParam String 
userName, 
Model model) {    
if(userID != null){
//..
}else{
// ...
}