Handling special characters in REST API
18,737
It is not nice, but you can use Base64 transformation.
So you client will send an encoded id
and you decode it back in your controller.
import org.apache.commons.codec.binary.Base64;
@RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
public String get(@PathVariable String id) {
final String realId = new String(new Base64(true).encodeBase64URLSafe(id));
[...]
}
EDIT: You should use url save implementation of Base64:
- javascript: js-base64
- java: Base64 from Apache Commons
- Is it ok to remove newline in Base64 encoding
Related videos on Youtube
Author by
SuhasD
Updated on June 04, 2022Comments
-
SuhasD over 1 year
I am having following URL
http://context_path/info/abc/def
which gets converted like
http://context_path/info/abc%2Fdef
Where as my controller mapping is:
@RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
here 'id' contains forward slash (/). So when I hit the URL I get 400 Bad Request.
I know the possible solutions
- Setting tomcat to allow forward slash.
- Use URL encoding.
- Use of
@RequestParam
instead of@PathVariable
.
But above solution are not possible for me.
Q. Is there any other solution like (using regular expression or changing the mapping or anything else) to the problem ? Also how the tomcat treats other special characters ('.' , ''' , ':' , ',') correctly and controller gets hit but not for '/'.
Tried but not working :
1) @RequestMapping(value = "/info/{id:.*}", method = RequestMethod.GET) 2) @RequestMapping(value = "/info/{id}/**", method = RequestMethod.GET) 3) @RequestMapping(value = "/info/**", method = RequestMethod.GET)
-
user1211 almost 7 yearsPossible duplicate of Match the "Rest of the URL" using Spring 3 RequestMapping Annotation
-
ScanQR almost 7 yearsBut why there is a slash in ur id value?
-
SuhasD almost 7 years@TechBreak I know its wrong. but I am working on old values and I can not modify those.
-
SuhasD almost 7 years@user3632894 tried both the approaches but not working
-
ScanQR almost 7 years@SuhasD just clarification, you have ability to work over url on client side and then call server?
-
SuhasD almost 7 years@TechBreak I would prefer server side solution over client-server solution
-
SuhasD almost 7 yearsi added following Reg-EX [\[\]A-Za-z0-9 \"',():._^-]+. It works for all other special characters except '/' and if I modify above Reg-EX like [\\/\[\]A-Za-z0-9 \"',():._^-]+ then no URL is working (even those which are working with old Reg-EX)
-
ScanQR almost 7 yearsThis won't work. Spring will decode or unescape the mapping url and then try to invoke the handler method based on resulting URL. Therefore it will return as bad request.
-
dieter almost 7 years@TechBreak did you tried it? I'm tried right now and it works.
-
SuhasD almost 7 years@dit Base64 works well but just issue it if the id contains '?' then it converts it into '/'. Any other encoder who doesnt use '/' for encoding ?
-
dieter almost 7 years@SuhasD use
URL save base64
implementation from Apache: stackoverflow.com/questions/5641303/base64url-in-java -
SuhasD almost 7 years@dit new Base64(true) works for me. thanx. I used .encodeBase64URLSafe() method instead of just .encode() to remove "\r" and "\n". stackoverflow.com/questions/19952621/…