【问题标题】:Mule json schema validationMule json 模式验证
【发布时间】:2016-08-23 14:13:26
【问题描述】:

我正在尝试在 Mule 流中使用 Validate JSON 模式组件,并且我正在为传递的 json 获取 com.fasterxml.jackson.core.JsonParseException。下面是 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>

【问题讨论】:

  • Muleflow:

标签: json mule mule-component


【解决方案1】:

JSON 和 Schema 中都有一些错误如下:-
"price": 12.50后面多了一个逗号,
所以有效的 JSON 将是:-

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

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

在 JSON Schema 文件 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"]
}  

试试吧..它会很好的:)

【讨论】:

  • 感谢@Anirban-sen-chowdhary......它也可以保持“exclusiveMinimum”:true。
【解决方案2】:

你传递的 json 不正确。在价格:12.50 之后有一个额外的逗号“,”。即使在第二个价格元素之后,也会添加一个额外的逗号。 只需将其删除,它就可以正常工作。

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 2011-06-08
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多