3

I have a problem with sending umlauts to Spring API. I want to post a following JSON:

{
 "username": "testümlaut",
 "firstName": "test",
 "lastName": "Test"
}

for this I have a following method start:

 @RequestMapping(value="/User", method=RequestMethod.POST,
        produces={"application/json ; charset=utf-8"}
)
  @ResponseStatus(HttpStatus.CREATED)
  public @ResponseBody User postUser(@RequestBody User user) {      

    User user = userDao.addUser(user);

    return user;
  }

As you can see I do have a line:

produces={"application/json ; charset=utf-8"} 

but it does not help. I get always an exception (0xfc is ü):

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Invalid UTF-8 start byte 0xfc
 at [Source: java.io.PushbackInputStream@721e5ed1; line: 2, column: 19] (through reference chain: de.escosautomation.restserver.model.user.UserClone["username"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 start byte 0xfc
 at [Source: java.io.PushbackInputStream@721e5ed1; line: 2, column: 19] (through reference chain: de.escosautomation.restserver.model.user.UserClone["username"])

What can I also add to make it work?

Thanx.

user2957954
  • 1,221
  • 2
  • 18
  • 39

2 Answers2

3

You can check a few options here:

  • Encoding on your application server. For example on Tomcat, on all connectors make sure you have URIEncoding set to UTF-8. E.G.:

    <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" URIEncoding="UTF-8"/>

  • As you send a JSON, you need to have both consumes and produces set to MediaType.APPLICATION_JSON_UTF8_VALUE
    (application/json;charset=UTF-8). E.G:

@RequestMapping(value="/User", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_UTF8_VALUE, produces=MediaType.APPLICATION_JSON_UTF8_VALUE )

  • In web.xml:

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

yyunikov
  • 5,719
  • 2
  • 43
  • 78
  • done (i have a jetty server and do not know how to configure encoding as a property for all the requests). doesn't help :( – user2957954 Jul 27 '16 at 08:31
0

Actually I accept the answer of Yuri Yunikov.

But if someone is using SOAPui, don't forget to set the Encoding of request also there: like it is described here

Community
  • 1
  • 1
user2957954
  • 1,221
  • 2
  • 18
  • 39