简体   繁体   中英

Encoding Decoding umlauts for REST WS

I have a REST Web Service (Java-Spring) that takes HTTP GET parameters. The parameters can contain some local data for example Norwegian or German names or streets: eg street=Rübenkamp

I wonder is there a standard protocol that defines how local/special characters have to be handled by REST WebService and its Clients?

What I am looking for is general guidelines, for example:

  1. Client needs to encode the query parameter values using UTF-8/16
  2. On the server side the parameter values have to be first decoded using the encoding available on the XXX (Request parameter?)
  3. When the server responds it first need to Encode the parameter values using UTF-8/16 and put the encoding in some header etc

Are there any rules for the above?

Rules for escaping are welcome as well.

Use org.springframework.web.filter.CharacterEncodingFilter

 <filter>
        <filter-name>SetCharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SetCharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

This must be first filter to be executed.

Also in your HTMl header you will like to have char encoding specified, then you will not mannualy need to do encoding or decoding.

Also go through http://forum.springsource.org/showthread.php?14063-How-to-set-setCharacterEncoding-on-Request

POST and GET parameters usually correspond to the Charset settings of the request's sender. So in order to make sure the sender does not use some exotic setting you can force it to send the request as UTF-8 (which by the way is pretty much recommended for almost every case):

request.setCharacterEncoding("UTF-8");
or
request.setContentType("text/html; charset=UTF-8")

If you can't force the client to use UTF-8 as encoding, then you might have to check if the encoding it used is compatible to UTF-8 and "translate" it

request.getCharacterEncoding().equals("UTF-8");

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