请在使用Java验证JSON Schema之前建议如何执行类型转换。我在NodeJS中使用json-schema-validation-pipeline包实现了相同的功能。下面的代码片段供参考(其中param1实际上是后端API提供的字符串类型)。
var ValidationPipeline = require('json-schema-validation-pipeline');
var V = ValidationPipeline.V;
var validate = ValidationPipeline([
{
$schema: {
'param1': V(Number).min(60)
}
},
{ $cast: { param1: Number } }
]);
所以基本上,我正在寻找Java上的等效解决方案,用于上面的代码片段。谢谢
将它分配给JAVA的POJO模型类,一旦你有了这个本机对象,那么你就可以在语言操作中对任何东西进行类型转换。例如 -
File file = new File("json/student.json");
// get json as buffer
BufferedReader br = new BufferedReader(new FileReader(file));
// obtained Gson object
Gson gson = new Gson(); //import com.google.gson.Gson;
// called fromJson() method and passed incoming buffer from json file
// passed student class reference to convert converted result as Student object
Student student = gson.fromJson(br, Student.class);