Reputation: 59
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.
Upvotes: 3
Views: 446
Reputation: 7543
From very beginning I really wondered why your solution doesn't work.
I've checked path variables collected into map - they work correctly. I think your issue was with dispatcher servlet. You can try configure one or search some existing solutions, I propose to use solution from spring box.
Please check my solution on git. Pay attention on 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 is few row of your code, so should be clear for you. But if something is not clear - do not hesitate and ask in comment.
Upvotes: 1
Reputation: 47895
According to the Spring docs:
A @PathVariable argument can be of any simple type such as int, long, Date, etc.
(Although this behaviour can be customised).
In addition, your controller specifies a URI with two parameters:
@RequestMapping(value="/hi/{cn}/{un}")
But the controller method only declares one: @PathVariable Map<String, String> pathVars
.
If your goal is (as this suggests: "trying out some simple examples") to prove some front to back calls using Spring MVC then perhaps you should keep things simple at first, for example:
@RequestMapping(value="/hi/{cn}/{un}")
public ModelAndView hi(@PathVariable String cn, @PathVariable String un){
...
}
Upvotes: 0
Reputation: 3522
@PathVariable
can use Map<String,String>
, you need to use <mvc:annotation-driven/>
into ApplicationContaxt.xml
. you can find 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/>
Upvotes: 1