I have problem with @PathVariable in SpringMVC controller. Whenever I pass string containing plus ('+'), plus gets replaced by space. Encoding the parameter doesn't help.
So for example if I request url myapp/resend-validation/[email protected], I get "my [email protected]" in my email
variable. Same happens after requesting myapp/resend-validation/my%[email protected]
My controller looks like this:
@RequestMapping(value = "/resend-validation/{email:.+}")
public String resendValidation(@PathVariable String email, HttpServletRequest request, Model model) {
//controller code here
}
(the regular expresion in @RequestMapping's value is because of the dot in the email address, otherwise, value gets truncated after the dot. It doesn't affect my problem)
Thanks in advance for any help.
The @PathVariable annotation is used to extract the value of the template variables and assign their value to a method variable. A Spring controller method to process above example is shown below; @RequestMapping("/users/{userid}", method=RequestMethod.
@PathVariable is a Spring annotation which indicates that a method parameter should be bound to a URI template variable. If the method parameter is Map<String, String> then the map is populated with all path variable names and values.
The @PathVariable annotation is used to extract the value from the URI. It is most suitable for the RESTful web service where the URL contains some value. Spring MVC allows us to use multiple @PathVariable annotations in the same method. A path variable is a critical part of creating rest resources.
While @RequestParams extract values from the query string, @PathVariables extract values from the URI path: @GetMapping("/foos/{id}") @ResponseBody public String getFooById(@PathVariable String id) { return "ID: " + id; } Then we can map based on the path: http://localhost:8080/spring-mvc-basics/foos/abc ---- ID: abc.
Silly me! It was error in completely different part. I'm calling the controller using Ajax with some javascript processing involved and the bug is there - it replaces 'plus' with 'space' and than calls the server. Thanks everyone for their time with this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With