Why is Spring de-coding + (the plus character) on application/json get requests? and what should I do about it?

前端 未结 3 1621
生来不讨喜
生来不讨喜 2020-12-18 01:16

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

3条回答
  •  有刺的猬
    2020-12-18 01:48

    SPR-6291 fixed this problem in v3.0.5 but this remains unresolved in some other cases like SPR-11047 is still unresolved. While SPR-6291's priority was Major, SPR-11047's priority is Minor.

    I faced this problem when I was working on REST API in old Spring last year. There are multiple ways we can get data in Spring controller. So two of them are via @RequestParam or @PathVariable annotation

    As others mentioned I think its spring's internal issue and does not specifically belong to URL encoding because I was sending data over POST request but it is somewhat encoding problem. But I also agree with others as now it remains problematic only in URL.

    So there are two solutions I know:

    1. You can use @PathVariable instead of @RequestParam because as of SPR-6291 this plus sign issue is fixed in @PathVariable and still remains open for @RequestParam as SPR-11047

    2. My version of spring was not even accepting plus sign via @PathVariable annotation, so this is how I overcome the problem (I don't remember it step by step but it will give you hint).

    In your case you can get the fields via JS and escape the plus sign before sending a request. Something like this:

    var email = document.getElementById("emailField").value;
    email = email.replace('+', '%2B');
    

提交回复
热议问题