0

Getting the below exception while parsing the JSON request body,

The request body has a special character "®" (non UTF-8) and while parsing, this fails. How to handle this non UTF-8 characters in the request body?

{
    "fields": [
        {
            "fieldLabel": "data®"
        }
    ]
}

I want to save the object with the special character. I tried researching and couldn't find one proper solution. Thanks in advance.

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Invalid UTF-8 start byte 0xae; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 start byte 0xae
 at [Source: (PushbackInputStream); line: 1, column: 907] (through reference chain: java.util.ArrayList[0]->com.model.TableDefinition["field"]->java.util.ArrayList[1]->com.model.FieldDefinition["fieldLabel"])
SwagiWagi
  • 411
  • 5
  • 17
Vignesh_A
  • 508
  • 1
  • 7
  • 19
  • 3
    JSON must be encoded in UTF8. Can you fix whatever is making the request so it sends correct JSON? – Joni Jul 20 '20 at 11:13
  • 3
    There is no such thing as a "non-UTF-8 character". Any Unicode character can be encoded with UTF-8. Your JSON is apparently encoded with a different character encoding than UTF-8 - which is strange and unusual, and probably means there is a bug in whatever system that produced this JSON. If you can't fix the bug in that other system, then you could work around it by parsing the JSON using the *actual* character encoding that it's in, instead of UTF-8. – Jesper Jul 20 '20 at 11:23
  • 1
    In ISO-8859-1 encoding (and many other ISO-8859-xx encodings), ® is encoded as 0xae. Chances are the request body is using ISO-8859-1 or similar. Even though non-Unicode encodings are very rare for JSON, it might make sense to check the content type of the request. It should indicates the encoding as `application/json; charset=iso-8859-1`. – Codo Jul 20 '20 at 11:44
  • I found the issue to be in the source system which produced json, I have added the following encoding stuff in the headers which resolved the problem "headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString()+ ";charset=UTF-8"); " – Vignesh_A Jul 20 '20 at 11:54
  • Does this https://stackoverflow.com/questions/39939555/how-to-config-spring-boot-application-to-support-both-utf-8-and-gbk-encode help you? – Rahul Kumar Jul 20 '20 at 12:05

1 Answers1

1

Adding "charset=UTF-8" to the content type in the headers resolved the issue :

headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON.toString()  + ";charset=UTF-8")
Vignesh_A
  • 508
  • 1
  • 7
  • 19