Spring mvc @PathVariable gives "The request sent by the client was syntactically invalid".

Hi I'm new to Spring MVC and trying some simple examples.

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

I am trying to hit the url and access PathVariables using @PathParam on 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 config 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 output.

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

I tried several options and also tried to change the spring-webmvc version to 4.1.6.RELEASE, but in this case I get 404.

Any help would be great. Thanks in advance.

+3


source to share


2 answers


@PathVariable

can use Map<String,String>

, you need to use <mvc:annotation-driven/>

in ApplicationContaxt.xml

. you can find the code below:



<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/>

      

+1


source


From the very beginning, I really wondered why your solution was not working.

I checked the path variables collected in the map - they work correctly. I think your problem is with the dispatch servlet. You can try to customize one or find some existing solutions, I suggest using a solution from spring.

Please check my solution on git. Pay attention to the build.gradle

file

dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}

      



...

dependencies {
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    runtime('org.springframework.boot:spring-boot-devtools')
}

      

Link to my git project solution

There are multiple lines of your code, so it should be clear to you. But if something is unclear - do not hesitate and ask in the comments.

+1


source







All Articles