POST 请求不接受 JSON 的特定格式,即使它是相同的

POST request doesn't accept a specific formatation of JSON, even though it's the same

我正在尝试使用以下请求主体向我的 API 发出 POST 请求:

{
    "data": [
        {
            "source": "A",
            "target": "B",
            "distance": 10
        },
        {
            "source": "A",
            "target": "C",
            "distance": 15
        }
    ]
}

{​
  "data": [
    {​
      "source": "A", "target": "B", "distance": 6
    }​,
    {​
      "source": "A", "target": "E", "distance": 4
    }
  ]
}

第一个有效,但第二个无效,returns出现以下错误:

"message": "JSON parse error: Unexpected character ('​' (code 8203 / 0x200b)): was expecting double-quote to start field name; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('​' (code 8203 / 0x200b)): was expecting double-quote to start field name\n at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 5]"

这是我的控制器的 POST 功能:

@PostMapping()
    public ResponseEntity<Graph> saveGraph(@RequestBody Graph graph){
        System.out.println(graph);
        Graph obj = graphService.saveGraph(graph);
        return ResponseEntity.status(HttpStatus.CREATED).body(obj);
    }

这是为什么?

0x200b is the Unicode value for a zero-width space,Jackson JSON 解析器似乎无法处理。 Zero-width 默认情况下,大多数编辑器中通常不显示空格,这会导致像您遇到的那样令人困惑的情况。

您的文本或代码编辑器可能具有显示隐藏字符的设置;我将你的两个片段都粘贴到 Sublime Text 中,它向我显示了导致问题的隐藏字符:

删除第二个 JSON 样本中的四个 zero-width 字符应该可以解决错误,手动或通过 运行 JSON 样本通过正则表达式去除在用 Jackson 解析之前输出 0x200b 值。