Sal81
Sal81

Reputation: 101

Grails not encoding in UTF-8 after posting body with contentType application/json

Currently I'm working in a grails application that is using rest calls to access another application using JSON messages.

When I try to submit a form with umlauts characters (üöä) the encoding of the body for the post is not UTF-8. Because of that the REST application is throwing a Json parse exception.

This is the grails call to the REST app.

withRest(uri: "${url}/service/update") {
    def response = post(body: javaClassA, requestContentType: ContentType.JSON)
    return response.data
}

This is how the grails applications is formating the fields from javaClassA, this snipet has been taken from the grails logs (you can see the umlaut characters are not encoded in UTF-8)

DEBUG http.wire - >> "{"content":"[0xfc][0xf6][0xe4]",...}"

And next the exception in the REST app.

org.codehaus.jackson.JsonParseException: Invalid UTF-8 start byte 0xfc

I have made all the configurations needed in order to set the encoding to UTF-8 in grails, following the next links.

http://www.oodlestechnologies.com/blogs/Configuring-Grails-App-for-UTF8-Character-Encoding

How to get UTF-8 working in Java webapps?

System specifications: grails app (version 2.3.4), emebedded tomcat (version 7.0.47), Java 1.7.0_25, maven 3.1.1, SO Windows 7.

Also the browser encoding and local file encoding looks oka. And also the JVM is using -Dfile.encoding=UTF8

I have run out of ideas of why this could be happening.

Help is more than welcome.

Thnks.

Upvotes: 1

Views: 2445

Answers (1)

Sal81
Sal81

Reputation: 101

Solution:

ParserRegistry.setDefaultCharset("UTF-8");

Add the previous line to BootStrap.groovy just after line

def init = { servletContext ->

Upvotes: 1

Related Questions