首页 > 解决方案 > Camel Json 验证抛出 NoJsonBodyValidationException

问题描述

我正在尝试对传入的 GET 请求执行标头验证。我引用了Camel JSON 模式验证器组件并按照以下步骤在我的项目中实现,即

运行项目会抛出一个org.apache.camel.component.jsonvalidator.NoJsonBodyValidationException,我正在使用 GET 请求,但为什么它需要请求正文,我只是想验证标头并从传入请求中请求参数。我不确定我的方法是否正确以及我缺少什么。

标签: restvalidationapache-camelopenapispring-camel

解决方案


去年我在采用 OpenAPI 时遇到了这个问题,得出的结论是工作量太大。我无法使用 OpenAPI 从 JSON 验证器获得 FULL 验证,因为 OpenAPI 声明模式定义的方式与完整的 JSON 模式定义之间存在一些差异。

查看 JSON 验证组件的文档,您会发现:

JSON Schema Validator 组件使用 NetworkNT JSON Schema 库 ( https://github.com/networknt/json-schema-validator )对 JSON Schemas v4 草案执行消息正文的 bean 验证。这是一个完全独立的 JSON Schema,如果您阅读 github 页面,您会发现它。

OpenAPI 支持

OpenAPI 3.0 规范使用 JSON 模式来验证请求/响应,但存在一些差异。使用配置文件,您可以使库与 OpenAPI 3.0 验证一起使用。

OpenAPI 模式似乎是真实 JSON 模式的一个子集。

在我向您展示一个更详细的示例之前。在此处查看骆驼文档中给出的示例:https ://camel.apache.org/components/latest/json-validator-component.html 。将该 json 模式文件与 openAPI 模式定义进行比较,您会发现它们并不相同。

这里一个有用的工具是 https://jsonschema.net,您可以在此处粘贴您的 json 示例并推断模式。我在下面的示例中使用了这个工具和 OpenAPI Pet Store 示例,

OpenAPI Petstore 宠物对象示例:

{
  "id": 0,
  "category": {
    "id": 0,
    "name": "string"
  },
  "name": "doggie",
  "photoUrls": [
    "string"
  ],
  "tags": [
    {
      "id": 0,
      "name": "string"
    }
  ],
  "status": "available"
}

保存在 JSON 中的 openAPI 规范产生了这个定义:

  "Pet": {
      "type": "object",
      "required": [
        "name",
        "photoUrls"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64"
        },
        "category": {
          "$ref": "#/definitions/Category"
        },
        "name": {
          "type": "string",
          "example": "doggie"
        },
        "photoUrls": {
          "type": "array",
          "xml": {
            "name": "photoUrl",
            "wrapped": true
          },
          "items": {
            "type": "string"
          }
        },
        "tags": {
          "type": "array",
          "xml": {
            "name": "tag",
            "wrapped": true
          },
          "items": {
            "$ref": "#/definitions/Tag"
          }
        },
        "status": {
          "type": "string",
          "description": "pet status in the store",
          "enum": [
            "available",
            "pending",
            "sold"
          ]
        }
      },
      "xml": {
        "name": "Pet"
      }
    }

当我将其转换为正确的 JSON 模式语法时,JSON 模式如下所示:

{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/root.json",
  "type": "object",
  "title": "The Root Schema",
  "required": [
    "id",
    "category",
    "name",
    "photoUrls",
    "tags",
    "status"
  ],
  "properties": {
    "id": {
      "$id": "#/properties/id",
      "type": "integer",
      "title": "The Id Schema",
      "default": 0,
      "examples": [
        0
      ]
    },
    "category": {
      "$id": "#/properties/category",
      "type": "object",
      "title": "The Category Schema",
      "required": [
        "id",
        "name"
      ],
      "properties": {
        "id": {
          "$id": "#/properties/category/properties/id",
          "type": "integer",
          "title": "The Id Schema",
          "default": 0,
          "examples": [
            0
          ]
        },
        "name": {
          "$id": "#/properties/category/properties/name",
          "type": "string",
          "title": "The Name Schema",
          "default": "",
          "examples": [
            "string"
          ],
          "pattern": "^(.*)$"
        }
      }
    },
    "name": {
      "$id": "#/properties/name",
      "type": "string",
      "title": "The Name Schema",
      "default": "",
      "examples": [
        "doggie"
      ],
      "pattern": "^(.*)$"
    },
    "photoUrls": {
      "$id": "#/properties/photoUrls",
      "type": "array",
      "title": "The Photourls Schema",
      "items": {
        "$id": "#/properties/photoUrls/items",
        "type": "string",
        "title": "The Items Schema",
        "default": "",
        "examples": [
          "string"
        ],
        "pattern": "^(.*)$"
      }
    },
    "tags": {
      "$id": "#/properties/tags",
      "type": "array",
      "title": "The Tags Schema",
      "items": {
        "$id": "#/properties/tags/items",
        "type": "object",
        "title": "The Items Schema",
        "required": [
          "id",
          "name"
        ],
        "properties": {
          "id": {
            "$id": "#/properties/tags/items/properties/id",
            "type": "integer",
            "title": "The Id Schema",
            "default": 0,
            "examples": [
              0
            ]
          },
          "name": {
            "$id": "#/properties/tags/items/properties/name",
            "type": "string",
            "title": "The Name Schema",
            "default": "",
            "examples": [
              "string"
            ],
            "pattern": "^(.*)$"
          }
        }
      }
    },
    "status": {
      "$id": "#/properties/status",
      "type": "string",
      "title": "The Status Schema",
      "default": "",
      "examples": [
        "available"
      ],
      "pattern": "^(.*)$"
    }
  }
}

OpenAPI 的架构定义和 JSON 架构定义之间存在一些差异。


推荐阅读