I have a Spring application that receives a request like http://localhost/foo?email=foo+bar@example.com
. This triggers a controller that roughly looks like this:
@RestController
@RequestMapping("/foo")
public class FooController extends Controller {
@GetMapping
public void foo(@RequestParam("email") String email) {
System.out.println(email)
}
}
By the time I can access email
, it's been converted to foo bar@example.com
instead of the original foo+bar@example.com
. According to When to encode space to plus (+) or %20? this should only happen in requests where the content is application/x-www-form-urlencoded
. My request has a content type of application/json
. The full MIME headers of the request look like this:
=== MimeHeaders ===
accept = application/json
content-type = application/json
user-agent = Dashman Configurator/0.0.0-dev
content-length = 0
host = localhost:8080
connection = keep-alive
Why is Spring then decoding the plus as a space? And if this is the way it should work, why isn't it encoding pluses as %2B
when making requests?
I found this bug report about it: https://jira.spring.io/browse/SPR-6291 which may imply that this is fixed on version 3.0.5 and I'm using Spring > 5.0.0. It is possible that I may misinterpreting something about the bug report.
I also found this discussion about RestTemplate treatment of these values: https://jira.spring.io/browse/SPR-5516 (my client is using RestTemplate).
So, my questions are, why is Spring doing this? How can I disable it? Should I disable it or should I encode pluses on the client, even if the requests are json?
Just to clarify, I'm not using neither HTML nor JavaScript anywhere here. There's a Spring Rest Controller and the client is Spring's RestTemplate
with UriTemplate
or UriComponentsBuilder
, neither of which encode the plus sign the way Spring decodes it.