使用工具把要check的json转换成json-schema
可以使用json-schema-validator来做json-schema的校验
fun checkJsonData(json: String,schema:String) {
val start = System.currentTimeMillis()
val jsonSchemaFacory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)
val objectMapper = ObjectMapper()
val schemaNode: JsonNode = objectMapper.readTree(schema)
val validationFor: JsonNode = objectMapper.readTree(json)
val jsonSchema: JsonSchema = jsonSchemaFacory.getSchema(schemaNode)
val errorMessage: Set<ValidationMessage> = jsonSchema.validate(validationFor)
for (error in errorMessage) {
logger.info(JSON.toJSONString(error))
if (error != null) {
val msg = error.message.replace("\$.", "")
logger.info(msg)
throw Exception(msg)
}
}
}