I have taken 2 PathVariable, and instead of taking these separately i want to store these 2 PathVariables in to a Map and want to retrieve it from the Map.
In Spring MVC 3.1.0 here is the my Controller class method:
@Controller
@RequestMapping("/welcome")
public class HelloController {
@RequestMapping(value="/{countryName}/{userName}",method=RequestMethod.GET)
public String getPathVar(@PathVariable Map<String, String> pathVars, Model model) {
String name = pathVars.get("userName");
String country = pathVars.get("countryName");
model.addAttribute("msg", "Welcome " + name+ " to Spring MVC & You are from" + country);
return "home";
}
My Request URL is : http://localhost:3030/spring_mvc_demo/welcome/India/ashrumochan123
But when giving request using this url, i am getting HTTP Status 400 -
Description: The request sent by the client was syntactically incorrect.
When i am taking these Path Variables separately then it is working fine. Here is the code -
@RequestMapping(value="/{countryName}/{userName}", method=RequestMethod.GET)
public String goHome(@PathVariable("countryName") String countryName,
@PathVariable("userName") String userName, Model model) {
model.addAttribute("msg", "Welcome " + userName
+ " to Spring MVC& You are from " + countryName);
return "home";
}
Please tell me whether i am doing anything wrong?
Any help would be greatly appreciated.
Stringinto aMap, which is named inconsistently in your annotation and method signature. Why not just create the map in the controller method from the two parameters?If the method parameter is Map<String, String> or MultiValueMap<String, String> then the map is populated with all path variable names and values. Since: 3.0