Mule json 模式验证

Mule json schema validation

我正在尝试在 Mule 流中使用验证 JSON 模式组件,我得到 com.fasterxml.jackson.core.JsonParseException 用于传递的 json。下面是 Mule 流的 json 架构、json 示例和代码。你能指出我哪里做错了吗?

Json 架构:

{
   "$schema": "http://json-schema.org/draft-04/schema#",
   "title": "Product",
   "description": "A product from Acme's catalog",
   "type": "object",

   "properties": {

      "id": {
         "description": "The unique identifier for a product",
         "type": "integer"
      },

      "name": {
         "description": "Name of the product",
         "type": "string"
      },

      "price": {
         "type": "number",
         "minimum": 0,
         "exclusiveMinimum": true
      }
   },

   "required": ["id", "name", "price"]
}

Json 传递给 POST 方法:

[
   {
      "id": 2,
      "name": "An ice sculpture",
      "price": 12.50,
   },

   {
      "id": 3,
      "name": "A blue mouse",
      "price": 25.50,
   }
]

错误:

Root Exception stack trace:
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('}' (code 125)): was expecting double-quote to start field name
 at [Source: java.io.InputStreamReader@6e7f030; line: 6, column: 5]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1419)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:508)

骡子流:

<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="jsonschemavalidationFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" allowedMethods="POST" doc:name="HTTP"/>
        <json:validate-schema schemaLocation="jsonschema.json" doc:name="Validate JSON Schema"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
    </flow>

json 你的路过是不对的。它在 price: 12.50 之后多了一个逗号 ','。即使在第二个价格元素之后,也会添加一个额外的逗号。 只需将其删除即可正常工作。

JSON 和架构中几乎没有错误,如下所示:-
"price": 12.50
后多了一个逗号, 所以有效的 JSON 将是:-

[
   {
      "id": 2,
      "name": "An ice sculpture",
      "price": 12.50
   },

   {
      "id": 3,
      "name": "A blue mouse",
      "price": 25.50
   }
]

在JSON架构文件jsonschema.json有两个错误:-
您需要输入“type": "array" 而不是 "type": "object"
接下来是 "exclusiveMinimum": true 似乎对这个 JSON 输入无效...
因此,正确的工作 JSON 架构将是:-

{
   "$schema": "http://json-schema.org/draft-04/schema#",
   "title": "Product",
   "description": "A product from Acme's catalog",
   "type": "array",

   "properties": {

      "id": {
         "description": "The unique identifier for a product",
         "type": "integer"
      },

      "name": {
         "description": "Name of the product",
         "type": "string"
      },

      "price": {
         "type": "number",
         "minimum": 0
      }
   },

   "required": ["id", "name", "price"]
}  

试一试..它会很好:)