1

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.

9
  • I don't think what you are looking to do is possible. For starters, you are trying coerce a String into a Map, which is named inconsistently in your annotation and method signature. Why not just create the map in the controller method from the two parameters? Commented Mar 10, 2016 at 1:35
  • @woemler, thank you for your quick response, but this is possible. I have seen some video demonstration regarding this. But confused about the version of Spring MVC they are using. Commented Mar 10, 2016 at 1:52
  • Actually @Ashrumochan was true,the method is correct but something else is wrong, I have tried and it work well for me, can I know what spring version you use? Commented Mar 10, 2016 at 2:22
  • From Spring doc: 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 Commented Mar 10, 2016 at 2:28
  • 1
    I'm using Spring 4.2.5.RELEASE, maybe you should check the client request, If problem still persist, try update your spring . Commented Mar 10, 2016 at 3:27

1 Answer 1

0

According to the Spring documentation, it's been there since version 3.2.

For the @PathVariable with Map<String,String>, I think you are missing <mvc:annotation-driven/> on your Spring servlet configuration:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<mvc:annotation-driven/>
...

I've found it on this link.

Sign up to request clarification or add additional context in comments.

3 Comments

i am using <mvc:annotation-driven/>
Can you provide your web and servlet xml? I couldn't reproduce this problem with only this information
Thanks @Auro Pimentel, Previously i was using Spring 3.1.1.RELEASE & now updated to Spring 4.1.6.RELEASE and now its working fine...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.