无效的UTF-8中间字节0x72

时间:2017-04-14 19:19:52

标签: jackson spring-kafka

我在spring-kafka中使用JsonSerializer和JsonDeserializer在生成消息时设置值序列化程序。 该消息有一个字段(orgName),其中包含一个特殊字符(德语变音符号)。如何处理这个特殊字符?我知道JsonDeserializer使用jackson和jackson支持utf-8。 JsonDeserializer因此引发此错误:

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]

2 个答案:

答案 0 :(得分:4)

确保使用-Dfile.encoding = UTF-8启动JVM。您JVM默认使用操作系统字符集

此处my tutorial on how to do that

答案 1 :(得分:2)

您的反序列化程序使用UTF-8,但您的Serializer使用其他编码,例如ISO 8859-1。要解决此问题,必须将Serializer的编码设置为UTF-8。

有关发生此错误的原因的解释,请参阅this answer以查找类似问题。