骆驼 Json 验证抛出 NoJsonBodyValidationException [英] Camel Json Validation throws NoJsonBodyValidationException

查看:34
本文介绍了骆驼 Json 验证抛出 NoJsonBodyValidationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to perform Header validation for incoming GET request. I referred the Camel JSON schema validator component and followed below steps to implement in my project i.e.

  • 在 build.gradle 中添加camel-json-validator-starter 依赖
  • 在我的 Spring Boot 项目的 Resource 文件夹中添加 Employee.json(YAML 转换为 JSON).这里最初我有 Open API 3.0 yaml 规范文件,并将其转换为 json
  • 使用以下代码调用验证

  • Adding camel-json-validator-starter dependency in build.gradle
  • Adding Employee.json (YAML converted to JSON) in Resource folder of my Spring boot project. Here initially I had Open API 3.0 yaml specification file and I converted the same to json
  • Invoking validation with below code

rest(/employee).id("get-employee")
    .produces(JSON_MEDIA_TYPE)
    .get()
    .description("The employee API")
    .outType(EmployeeResponse.class)
    .responseMessage()
      .code(HttpStatus.OK.toString())
      .message("Get Employee")
    .endResponseMessage()
    .route()
    .to("json-validator:openapi.json")
    .to("bean:employeeService?method=getEmployee()");

运行项目抛出一个 org.apache.camel.component.jsonvalidator.NoJsonBodyValidationException,我使用的是 GET 请求,但为什么它期待请求正文,我只是想验证标题和请求来自传入请求的参数.我不确定我的方法是否正确以及我缺少什么.

Running the project throws a org.apache.camel.component.jsonvalidator.NoJsonBodyValidationException, I'm using GET request but why is it expecting Request body, I just wanted to validate the Headers and request param from the incoming request. I'm not sure if my approach is right and what I'm missing.

推荐答案

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

I ran into this problem last year when adopting OpenAPI and came to the conclusion that it was too much work. I could not get FULL validation from the JSON validator using OpenAPI because there was some differences between the way OpenAPI declares schema definitions and the full JSON schema definitions.

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

Looking a the documentation of the JSON validation component you find this:

JSON Schema Validator 组件使用 NetworkNT JSON Schema 库 (https://github.com/networknt/json-schema-validator).这是一个完整的独立 JSON 模式,如果您阅读 github 页面,您会发现它.

The JSON Schema Validator component performs bean validation of the message body against JSON Schemas v4 draft using the NetworkNT JSON Schema library (https://github.com/networknt/json-schema-validator). This is a full stand alone JSON Schema and if you read the github pages you find this.

OpenAPI 支持

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

The OpenAPI 3.0 specification is using JSON schema to validate the request/response, but there are some differences. With a configuration file, you can enable the library to work with OpenAPI 3.0 validation.

OpenAPI 模式似乎是真正的 JSON 模式的子集.

OpenAPI schema appears to be a subset of the real JSON Schema.

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

Before I show you a more detailed example. Look at the example given in the camel documentation here: https://camel.apache.org/components/latest/json-validator-component.html. Compare that json schema file with the openAPI schema definitions and you will see they are not the same.

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

A useful tool here is https://jsonschema.net you can paste your json example here and infer a schema. I use this tool and the OpenAPI Pet Store example in the example below,

OpenAPI Petstore Pet 对象示例:

OpenAPI Petstore Pet Object Example:

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

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

The openAPI specification saved in JSON produces this definition:

  "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 模式如下所示:

When I convert this to proper JSON schema syntax the JSON Schema looks like this:

{
  "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 模式定义之间存在一些差异.

There is some differences between OpenAPI's schema definition and JSON Schema definition.

这篇关于骆驼 Json 验证抛出 NoJsonBodyValidationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆