I instruct my URL to send an Ajax request like that:
url += '/' + something + '/' + id;
var response;
$.ajax({
async : false,
type: 'DELETE',
url: url,
...
My removeId is a variable that includes UTF-8 character. I will handle that variable at Java side like that:
@RequestMapping(value = "/something/{id}", method = RequestMethod.DELETE)
public void myMethod(HttpServletResponse response, @PathVariable String id) {
...
However id variable at Java side is different from its original because UTF-8 characters changes to strange things.
How can I send UTF-8 characters from JavaScript side and transform it again at my Java side (Spring 3 with REST, my web server is Tomcat 7)?
PS 1: Even I don’t use encodeUriComponent it seems that my URL is encoding by itself?
PS 2: To make question more clear:
i.e. my id variable is araç and sent URL is: localhost:8080/sdfasf/ara%C3%A7
When I see that id variable has that value:
araç
instead of:
ara%C3%A7
Does Spring (or Tomcat) automatically do it? Is there any way to decode it automatically when it comes to controller as a path variable (I mean without writing anything as like:
URLDecoder.decode(id,"UTF-8");
it will be converted automatically)
The id value you see seems to be decoded using the iso-8859-1 charset instead of utf-8. The encoding of the path part of a url is not specified in java EE and there is no standard api to set it. For query parameters you can use request.setCharacterEncoding before accessing any parameters to have them decoded correctly. The
CharacterEncodingFilterdoes exactly that but has no influence on path parameters.To make this work in Tomcat you have to set the
URIEncodingattribute of theConnectorelement in itsserver.xmlto “utf-8”.All you ever wanted to know about character encoding in a java webapp can be found in this excellent answer to a similar question.