Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringMVC: @PathVariable value containing the plus (+) character

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.

like image 985
Kejml Avatar asked Feb 12 '14 15:02

Kejml


People also ask

What does @PathVariable annotation do?

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.

What does @PathVariable mean?

@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.

What is the use of @PathVariable annotation in Spring rest?

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.

What is @PathVariable and @RequestParam?

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.


1 Answers

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.

like image 182
Kejml Avatar answered Sep 28 '22 17:09

Kejml