问题
I am using JsonSerializer and JsonDeserializer in spring-kafka to set the value serializer while producing a message. The message has one field(orgName)with a special character in it (german umlaut).How do I handle this special character? I know JsonDeserializer uses jackson and jackson supports utf-8. The JsonDeserializer throws this error because of it:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 middle byte 0x72
at [Source: [B@403d4534; line: 1, column: 128]
at [Source: [B@403d4534; line: 1, column: 116] (through reference chain: com.abc-company.kafka.JobRequest["orgName"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:388) ~[jackson-databind-2.8.5.jar:2.8.5]
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:348) ~[jackson-databind-2.8.5.jar:2.8.5]
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.wrapAndThrow(BeanDeserializerBase.java:1599) ~[jackson-databind-2.8.5.jar:2.8.5]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:359) ~[jackson-databind-2.8.5.jar:2.8.5]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148) ~[jackson-databind-2.8.5.jar:2.8.5]
at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1626) ~[jackson-databind-2.8.5.jar:2.8.5]
at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1237) ~[jackson-databind-2.8.5.jar:2.8.5]
at org.springframework.kafka.support.serializer.JsonDeserializer.deserialize(JsonDeserializer.java:86) ~[spring-kafka-1.1.2.RELEASE.jar:na]
回答1:
Make sure to start you JVM with -Dfile.encoding=UTF-8. You JVM defaults to the operating system charset
Here's my tutorial on how to do that!
回答2:
Your Deserializer uses UTF-8, but your Serializer uses another encoding, for example, ISO 8859-1. To solve this problem, you must set the encoding of the Serializer to UTF-8.
For an explanation why this error occurs, see this answer to a similar question.
来源:https://stackoverflow.com/questions/43417909/invalid-utf-8-middle-byte-0x72