杰克逊的错误:不可接受的性格'' (0x0)不允许使用特殊字符

时间:2016-08-24 07:21:21

标签: java regex jackson yaml

我试图使用MySQL中的jackson来解析yaml数据但是我得到错误说有一个特殊字符。我得到的错误是这样的:

com.fasterxml.jackson.databind.JsonMappingException: special characters are not allowed 
[Source: java.io.StringReader@5528a42c; line: 13, column: 68] (through reference chain: com.app.ResultContentModel["opinion"])
...
Caused by: com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.YAMLException: special characters are not allowed
 at [Source: java.io.StringReader@5528a42c; line: 13, column: 68]
...
Caused by: unacceptable character '' (0x0) special characters are not allowed
in "'reader'", position 1027

我的依赖项文件如下所示:

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
</dependency>
<dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
        <version>2.8.1</version>
</dependency>

我尝试使用正则表达式替换它:

Pattern nonASCII = Pattern.compile("[^\\x00-\\x7f]");//("[^\\x00-\\x7f]");
resultContent = Normalizer.normalize(nonASCII.matcher(resultContent).replaceAll("");
ResultContentModel rc = mapper.readValue(resultContent, ResultContentModel.class);

但仍然没有奏效。我该怎么办?

1 个答案:

答案 0 :(得分:2)

您似乎希望将所有非ASCII +所有字符与0020(空格)的十六进制代码匹配。

使用

Pattern nonASCII = Pattern.compile("[^\\x00-\\x7f]+|[\\x00-\\x20]+");

\x00-\x20将匹配您需要移除的所有控制字符,而[^\x00-\x7f]将匹配非ASCII字符。

+量词将匹配1个或多个匹配项,以便删除/替换一次匹配的字符块。