JSON Dropwizard Rest 正文开头的无效字符 API

Invalid Characters at Start of JSON Body of Dropwizard Rest API

我使用 Dropwizard 和 Spring 创建了一个基本的 CRUD API。在我的响应对象的正文中,我收到以下内容:

 )]}',
 {
  "id":10,
  "initiator":2,
  "target":1,
  "statusId":1,
  "created":"2018-04-30T14:45:01.173"
 }

我放心地在测试期间使用 curl、postman 和编程方式检查了 API,无效字符 )]}', 始终存在。 Postman 似乎能够忽略它们并显示漂亮的打印输出,但请放心,我猜大多数 JSON 解析器都无法正确解析它。

问题:

  1. 它们是什么?
  2. 他们为什么会出现?
  3. 如何删除它们?

多年来我一直在编写 REST API,但我从未见过这样的东西。这是我第一次使用 dropwizard,所以我乐观地希望它是我错过的一些配置。

除了无效字符外,API 功能正常。

这是一个继承的代码库,其他APIs return这些字符也是。出于放心测试的目的,在处理响应之前会过滤掉无效字符。虽然这在短期内对我来说似乎是可以接受的解决方法,但从长远来看,API 的任何未来消费者都必须执行该解决方法,理想情况下,这将在 API 本身中得到修复。

不知道 DropWizard,但它可以防止 json-劫持。 在Spring中有一个MappingJackson2HttpMessageConverterclass.
具有相同的功能,但前缀不同 "{} &&"

/**
 * Indicate whether the JSON output by this view should be prefixed with "{} &&". Default is false.
 * <p>Prefixing the JSON string in this manner is used to help prevent JSON Hijacking.
 * The prefix renders the string syntactically invalid as a script so that it cannot be hijacked.
 * This prefix does not affect the evaluation of JSON, but if JSON validation is performed on the
 * string, the prefix would need to be ignored.
 */
public void setPrefixJson(boolean prefixJson) {
    this.prefixJson = prefixJson;
}

你可以理解这一点。

编辑 1: Spring 版本 4.2.0.RELEASE 之后,默认前缀已更新为 )]}',