Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring/Rest @PathVariable character encoding
    primarykey
    data
    text
    <p>In the environment I'm using (Tomcat 6), percent sequences in path segments apparently are decoded using ISO-8859-1 when being mapped to a @PathVariable.</p> <p>I'd like that to be UTF-8.</p> <p>I already configured Tomcat to use UTF-8 (using the URIEncoding attribute in server.xml).</p> <p>Is Spring/Rest doing the decoding on its own? If yes, where can I override the default encoding?</p> <p>Additional information; here's my test code:</p> <pre><code>@RequestMapping( value = "/enc/{foo}", method = RequestMethod.GET ) public HttpEntity&lt;String&gt; enc( @PathVariable( "foo" ) String foo, HttpServletRequest req ) { String resp; resp = " path variable foo: " + foo + "\n" + " req.getPathInfo(): " + req.getPathInfo() + "\n" + "req.getPathTranslated(): " + req.getPathTranslated() + "\n" + " req.getRequestURI(): " + req.getRequestURI() + "\n" + " req.getContextPath(): " + req.getContextPath() + "\n"; HttpHeaders headers = new HttpHeaders(); headers.setContentType( new MediaType( "text", "plain", Charset.forName( "UTF-8" ) ) ); return new HttpEntity&lt;String&gt;( resp, headers ); } </code></pre> <p>If I do an HTTP GET request with the following URI path:</p> <pre><code>/TEST/enc/%c2%a3%20and%20%e2%82%ac%20rates </code></pre> <p>which is the UTF-8 encoded then percent-encoded form of</p> <pre><code>/TEST/enc/£ and € rates </code></pre> <p>the output that I get is:</p> <pre><code> path variable foo: £ and ⬠rates req.getPathInfo(): /enc/£ and € rates req.getPathTranslated(): C:\Users\jre\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\TEST\enc\£ and € rates req.getRequestURI(): /TEST/enc/%C2%A3%20and%20%E2%82%AC%20rates req.getContextPath(): /TEST </code></pre> <p>which to me shows that Tomcat (after setting the URIEncoding attribute) does the right thing (see getPathInfo()), but the path variable is decoded still in ISO-8859-1.</p> <p><em>And the answer is</em>:</p> <p>Spring/Rest apparently uses the request encoding, which is a very strange thing to do, as this is about the <em>body</em>, not the URI. Sigh.</p> <p>Adding this:</p> <pre><code>&lt;filter&gt; &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;encoding&lt;/param-name&gt; &lt;param-value&gt;UTF-8&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; </code></pre> <p>fixed the problem. It really should be simpler.</p> <p><em>And actually, it's worse:</em></p> <p>If the method indeed <em>has</em> a request body, and that one isn't encoded in UTF-8, the additional forceEncoding parameter is needed. This seems to work, but I'm concerned it will cause more problems later on.</p> <p><em>Another approach</em></p> <p>In the meantime, I found out that it's possible to disable the decoding, my specifying</p> <pre><code>&lt;property name="urlDecode" value="false"/&gt; </code></pre> <p>...in which case the recipient can to the <em>right thing</em>; but of course this will make lots of other things harder.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload