骆驼Json验证抛出NoJsonBodyValidationException

时间:2019-10-06 05:08:08

标签: rest validation apache-camel openapi spring-camel

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

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

    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请求,但是为什么要期待Request正文,我只是想验证Header并从传入请求中请求参数。我不确定我的方法是否正确以及我缺少什么。

2 个答案:

答案 0 :(得分:1)

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

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

JSON Schema Validator组件使用NetworkNT JSON Schema库(https://github.com/networknt/json-schema-validator)根据JSON Schemas v4草案对消息正文执行bean验证。

转到上面的github会显示以下行: 一个支持json模式草稿v4的Java json模式验证器。它是我们light-4j微服务框架中的关键组件,可在运行时根据OpenAPI规范针对light-rest-4j和RPC模式针对light-hybrid-4j验证请求。

骆驼不是光休息的4j。

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

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

OpenAPI Petstore Pet对象示例:

{
  "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 Schema如下所示:

{
  "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架构定义之间存在一些差异。

答案 1 :(得分:0)

failOnNullBody(生产者)-如果没有主体,是否失败。

默认为true

尝试在通话中设置选项:

.to("json-validator:openapi.json?failOnNullBody=false")