Spring mvc @PathVariable gives 'The request sent by the client was syntactically incorrect.'

Viewed 297

Hi I am new to Spring MVC and trying out some simple examples.

I have created a new Spring mvc maven project in eclipse using the archtype 'spring-mvc-archtype'.

I am trying to hit a url and access the PathVariables using @PathParam in a Map < String, String >

Here is my controller class

@Controller
@RequestMapping(value="/greet")
public class HomeController {

@RequestMapping(value="/welcome/{countryName}/{userName}")
public ModelAndView test(@PathVariable("userName") String userName, @PathVariable("countryName") String countryName) throws IOException{
    ModelAndView mav = new ModelAndView("home");
    mav.addObject("mymessage", "Welcome To "+countryName+" , Spring MVC - "+userName);
    return mav;
}

@RequestMapping(value="/hi/{cn}/{un}")
public ModelAndView hi(@PathVariable Map<String, String> pathVars){

    String countryName = pathVars.get("cn");
    String userName = pathVars.get("un");

    ModelAndView mav = new ModelAndView("home");
    mav.addObject("mymessage", "Hi "+userName+" in "+ countryName);
    return mav;
}
}

And here is the Configuration class

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="org.gyanbang.kiran.SpringMvc")
public class MvcConfiguration extends WebMvcConfigurerAdapter{

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}   
}

So when I run this and hit http://localhost:8080/SpringMvc/greet/welcome/India/user1 it works fine and I get the desired result.

But on http://localhost:8080/SpringMvc/greet/hi/India/user1 it gives a 400 error with message 'The request sent by the client was syntactically incorrect.'

I have tried few options and also tried changing the spring-webmvc version to 4.1.6.RELEASE but it gives me 404 in that case.

Any help would be great. Thanks in advance.

3 Answers
Related