使用 Everit JSON 模式验证器解析模式异常时返回一个 JSON 对象数组
Returning an array of JSON objects when parsing a schema exception with Everit JSON Schema Validator
我目前正在将 JSON Schema Validator 与 Gson 结合使用来处理异常并验证对 API 发出的 json 请求。
在将请求与模式进行比较时,验证器可以 return 多个异常。存储库中的示例是:
try {
schema.validate(rectangleMultipleFailures);
}
catch (ValidationException e) {
System.out.println(e.getMessage());
e.getCausingExceptions().stream()
.map(ValidationException::getMessage)
.forEach(System.out::println);
}
我对 try catch
的实现(显然漏掉了问题)是:
try (InputStream inputStream = this.getClass().getResourceAsStream("SupplierSchemaIncoming.json")) {
JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
Schema schema = SchemaLoader.load(rawSchema);
// Throws a ValidationException if requestJson is invalid:
schema.validate(new JSONObject(requestJson));
}
catch (ValidationException ve) {
System.out.println(ve.toJSON().toString());
}
正如您在上面看到的,一种选择是 return 将所有错误作为一个 JSON。
{
"pointerToViolation": "#",
"causingExceptions": [{
"pointerToViolation": "#/name",
"keyword": "type",
"message": "expected type: String, found: Integer"
}, {
"pointerToViolation": "#/type",
"keyword": "type",
"message": "expected type: String, found: Integer"
}],
"message": "2 schema violations found"
}
但是,我对如何获取 return 一个 SchemaError
对象数组(下方)的异常感到困惑,我可以根据需要进行解析。
package domainObjects;
import com.google.gson.annotations.Expose;
public class SchemaError {
@Expose
String pointerToViolation;
@Expose
String keyword;
@Expose
String message;
public SchemaError() {}
public String getPointerToViolation() {
return pointerToViolation;
}
public void setPointerToViolation(String pointerToViolation) {
this.pointerToViolation = pointerToViolation;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
发现 建议将对象从 JSONObject 序列化为 JSONElement 并返回它。 catch()
现在有:
catch (ValidationException ve) {
JSONObject jsonObject = ve.toJSON();
Gson gson = new Gson();
JsonElement element = gson.fromJson(jsonObject.toString(), JsonElement.class);
return element;
}
我目前正在将 JSON Schema Validator 与 Gson 结合使用来处理异常并验证对 API 发出的 json 请求。
在将请求与模式进行比较时,验证器可以 return 多个异常。存储库中的示例是:
try {
schema.validate(rectangleMultipleFailures);
}
catch (ValidationException e) {
System.out.println(e.getMessage());
e.getCausingExceptions().stream()
.map(ValidationException::getMessage)
.forEach(System.out::println);
}
我对 try catch
的实现(显然漏掉了问题)是:
try (InputStream inputStream = this.getClass().getResourceAsStream("SupplierSchemaIncoming.json")) {
JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
Schema schema = SchemaLoader.load(rawSchema);
// Throws a ValidationException if requestJson is invalid:
schema.validate(new JSONObject(requestJson));
}
catch (ValidationException ve) {
System.out.println(ve.toJSON().toString());
}
正如您在上面看到的,一种选择是 return 将所有错误作为一个 JSON。
{
"pointerToViolation": "#",
"causingExceptions": [{
"pointerToViolation": "#/name",
"keyword": "type",
"message": "expected type: String, found: Integer"
}, {
"pointerToViolation": "#/type",
"keyword": "type",
"message": "expected type: String, found: Integer"
}],
"message": "2 schema violations found"
}
但是,我对如何获取 return 一个 SchemaError
对象数组(下方)的异常感到困惑,我可以根据需要进行解析。
package domainObjects;
import com.google.gson.annotations.Expose;
public class SchemaError {
@Expose
String pointerToViolation;
@Expose
String keyword;
@Expose
String message;
public SchemaError() {}
public String getPointerToViolation() {
return pointerToViolation;
}
public void setPointerToViolation(String pointerToViolation) {
this.pointerToViolation = pointerToViolation;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
发现 catch()
现在有:
catch (ValidationException ve) {
JSONObject jsonObject = ve.toJSON();
Gson gson = new Gson();
JsonElement element = gson.fromJson(jsonObject.toString(), JsonElement.class);
return element;
}