json schema 校验

依赖库:

<dependency>
  <groupId>com.github.java-json-tools</groupId>
  <artifactId>json-schema-validator</artifactId>
  <version>2.2.14</version>
</dependency>

该包还依赖:

<dependency>
    <groupId>com.github.java-json-tools</groupId>
    <artifactId>json-schema-core</artifactId>
    <version>1.2.14</version>
</dependency>

<dependency>
    <groupId>com.github.java-json-tools</groupId>
    <artifactId>jackson-coreutils</artifactId>
    <version>2.0</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.1</version>
</dependency>

json检查:

  • 检查json自身语法的错误。
  • 自定义json数据格式的校验。

json 语法检查

例如: 下面json文件格式错误,需要能报错。

{
  "firstName":"x",
  }
  "lastName":"y",
  "a": "test"
}

当加载该json内容时,应检查语法错误。(fastjson需要调用valiator检查,并且检查错误信息不详细)。

代码:

package com.ts.ap.toolchain.idl.generator.test;

import java.io.IOException;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;

public class TestMain2 {

  public static void main(String[] args) {
    JsonNode schema = null;
    JsonNode testData = null;
    try {
      schema = JsonLoader.fromPath("testData/json_schema/test_schema.json");
      testData = JsonLoader.fromPath("testData/json_schema/test_json_data_1.json");
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
  
    System.out.println(schema);
    System.out.println(testData);
  }
}

报错信息:

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('}' (code 125)): was expecting double-quote to start field name
 at [Source: (FileInputStream); line: 3, column: 4]
	at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1851)
	at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:707)

自定义 shema 校验

shema定义:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Example Schema",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
    "age": {
      "description": "Age in years",
      "type": "integer",
      "minimum": 0
    }
  },
  "required": ["firstName", "lastName"]
}

测试数据:

{
  "firstName":"x",
  "a": "test"
}

代码:

package com.ts.ap.toolchain.idl.generator.test;

import java.io.IOException;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;

public class TestMain2 {

  public static void main(String[] args) {
    JsonNode schemaNode = null;
    JsonNode testData = null;
    try {
      schemaNode = JsonLoader.fromPath("testData/json_schema/test_schema.json");
      testData = JsonLoader.fromPath("testData/json_schema/test_json_data_1.json");
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
  
    JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    JsonSchema schema = null;
    try {
      schema = factory.getJsonSchema(schemaNode);
    } catch (ProcessingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  
    try {
      ProcessingReport report = schema.validate(testData);
      if(!report.isSuccess()) {
        System.out.println(report.toString());
      }
    } catch (ProcessingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  
    System.out.println(schemaNode);
    System.out.println(testData);
  
  }
}

输出:

com.github.fge.jsonschema.core.report.ListProcessingReport: failure
--- BEGIN MESSAGES ---
error: object has missing required properties (["lastName"])
    level: "error"
    schema: {"loadingURI":"#","pointer":""}
    instance: {"pointer":""}
    domain: "validation"
    keyword: "required"
    required: ["firstName","lastName"]
    missing: ["lastName"]
---  END MESSAGES  ---

{"$schema":"http://json-schema.org/draft-04/schema#","title":"Example Schema","type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"age":{"description":"Age in years","type":"integer","minimum":0}},"required":["firstName","lastName"]}
{"firstName":"x","a":"test"}

设置 JsonSchema 的 ExceptionThreshold

代码:

    JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
      .setReportProvider(new ListReportProvider(LogLevel.INFO, LogLevel.WARNING))
      .freeze();

"additionalProperties":false 多余属性配置报错

shema

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Example Schema",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
    "age": {
      "description": "Age in years",
      "type": "integer",
      "minimum": 0
    }
  },
  "required": ["firstName", "lastName"],
  "additionalProperties":false
}

测试json:

{
  "firstName":"x",
  "lastName": "test",
  "a":1
}

代码:

package com.ts.ap.toolchain.idl.generator.test;

import java.io.IOException;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ListReportProvider;
import com.github.fge.jsonschema.core.report.LogLevel;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;

public class TestMain {

  public static void main(String[] args) {
  
    JsonNode schemaNode = null;
    JsonNode testData = null;
    try {
      schemaNode = JsonLoader.fromPath("testData/json_schema/test_schema.json");
      testData = JsonLoader.fromPath("testData/json_schema/test_json_data_1.json");
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
  
  
    JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
      .setReportProvider(new ListReportProvider(LogLevel.INFO, LogLevel.WARNING))
      .freeze();
//    JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    JsonSchema schema = null;
    try {
      schema = factory.getJsonSchema(schemaNode);
    } catch (ProcessingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  
    try {
      ProcessingReport report = schema.validate(testData);
      if(!report.isSuccess()) {
        System.out.println(report.toString());
      }
      System.out.println(report.getExceptionThreshold());
      System.out.println(report.toString());
  
    } catch (ProcessingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  
    System.out.println(schemaNode);
    System.out.println(testData);
  
  }
}

报错信息:

com.github.fge.jsonschema.exceptions.InvalidInstanceException: fatal: object instance has properties which are not allowed by the schema: ["a"]
    level: "fatal"
    schema: {"loadingURI":"#","pointer":""}
    instance: {"pointer":""}
    domain: "validation"
    keyword: "additionalProperties"
    unwanted: ["a"]

	at com.github.fge.jsonschema.keyword.validator.AbstractKeywordValidator$1.doException(AbstractKeywordValidator.java:49)
	at com.github.fge.jsonschema.core.report.ProcessingMessage.asException(ProcessingMessage.java:397)
	at com.github.fge.jsonschema.core.report.AbstractProcessingReport.dispatch(AbstractProcessingReport.java:176)
	at com.github.fge.jsonschema.core.report.AbstractProcessingReport.error(AbstractProcessingReport.java:131)
	at com.github.fge.jsonschema.keyword.validator.common.AdditionalPropertiesValidator.validate(AdditionalPropertiesValidator.java:101)

警告信息

Nashorn 要从JDK移除

参考文档: https://help.mulesoft.com/s/article/Nashorn-engine-is-planned-to-be-removed-from-a-future-JDK-release-warning

警告信息:

Warning: Nashorn engine is planned to be removed from a future JDK release

ISSUE DESCRIPTION

While using the Scripting module with the Java JDK 11, the application works fine but gives the below warning:

Warning: Nashorn engine is planned to be removed from a future JDK release

CAUSE

This is a warning to the developers who rely on Nashorn that it will be deprecated in future releases. The deprecation-for-removal of Nashorn in JDK 11 was confirmed in June 2018, causing the proposed removal to be flagged at every use of the jdk.nashorn.* API and the jjs tool in JDK 11, 12, and 13.

SOLUTION

This is expected since the Nashorn engine is no longer shipped with the JDK since JDK14 and the same can be found on the official OpenJDK documentation The warning should not have any impact on the application running on JDK 11.