简体   繁体   中英

How to suppress url encoding with spring boot

I have created a GET/POST API using Spring boot which has a http url parameter say refid. Now this parameter is already encoded before invoking GET/POST request eg http://localhost:8080/users/TESTFNkJXiQAH%2FJBKxigBx

But, when I deploy this through Spring Boot, the encoded refid is encoded again and the refid changes. ie it becomes:

http://localhost:8080/users/TESTFNkJXiQAH%252FJBKxigBx

I want to suppress this 2nd encoding by Spring boot. Can anyone advise here?

Don't know if you are still having this problem or you found out why it's happening, but because I was trying to explain to someone the phenomenon, I looked if there is already a good explanation. But since you also ask and I didn't find any, here is my answer.

So you encode your refid

TESTFNkJXiQAH%2FJBKxigBx

before you send it through the url, which then you give into a browser. Now this is only the encoded refid. When you call it through a URL directly you have to encode it again, according to the HTML URL encoding standards. That's why the double escape. Also read this . Eg so if your refid looks like this

test%123

and you encode it you turn it into

test%25123

now if you also want to pass it through a url on the browser you'd have to encode it again.

test%2525123

But if a service A is using this service and service A encodes this refid properly then you wont have this problem. It's happening only because you are trying to call this api endpoint through the browser.

Of course I take for granted that you are doing this:

String decoded = URLDecoder.decode(refid, "UTF-8");

in your controller

Pass the decoded URL in first place instead of doing inconvenient things to stop double encoding. You get already decoded field in rest controller. Example if you pass www.xyz.com?name=nilesh%20salpe

you will get value of param name as "nilesh salpe" and not "nilesh%20salpe"

This is a basic example of URLDecoder:

   @RequestMapping(value = "/users/{refId}", method = GET)
   public void yourMethod(@PathVariable("refId") String refId) {
      // This is what you get in Spring Boot
      String encoded = refId; //"TESTFNkJXiQAH%252FJBKxigBx"

      String decoded = URLDecoder.decode(encoded, "UTF-8");

      System.out.println(decoded);

      // Result TESTFNkJXiQAH%2FJBKxigBx
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM