骆驼Json验证抛出NoJsonBodyValidationException [英] Camel Json Validation throws NoJsonBodyValidationException
问题描述
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启动项目的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请求,但是为什么要期待Request正文,我只是想验证Header并从传入请求中请求参数.我不确定我的方法是否正确以及我缺少什么.
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库(
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).
转到上面的github会显示以下行: 一个支持json模式草稿v4的Java json模式验证器.它是我们light-4j微服务框架中的一个关键组件,用于在运行时根据针对light-rest-4j的OpenAPI规范和针对针对light-hybrid-4j的RPC模式验证请求.
Going to the github above reveals this line: A Java json schema validator that supports json schema draft v4. It is a key component in our light-4j microservices framework to validate request against OpenAPI specification for light-rest-4j and RPC schema for light-hybrid-4j at runtime.
骆驼不是光休息的4j.
Camel is not light-rest-4j.
在我向您展示更详细的示例之前.在这里查看骆驼文档中给出的示例: 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 Schema如下所示:
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屋!