【发布时间】:2026-01-10 09:15:01
【问题描述】:
我有一个 post API,我希望使用 JsonSchemaValidator 的方法,因为我希望验证整个响应,而不是通过执行断言来选择响应
我试过用
- matchesJsonSchemaInClasspath("我的文件名") 和
- matchesJsonSchema(我的文件对象)
我的 reposne 即将成为现实,方法正在通过,但我的架构文件没有检查或验证
public void directLoginWihSchemaValiadtor(){
File file = new File("C:/Users/abeey/git/SlingAppWebService/Configurations/JsonSchemaValidator_DirectLogin_AWS.json");
jsonasmap.put("contactNo", "some number");
jsonasmap.put("loginType","0");
jsonasmap.put("appid","2");
jsonasmap.put("co*****ode","IN");
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder().
setValidationConfiguration(ValidationConfiguration.newBuilder().freeze()).freeze();
given().contentType(ContentType.JSON).body(jsonasmap).when().
post("https://60i*****.execute-api.us-west-2.amazonaws.com/some-api").
then().assertThat().
body(JsonSchemaValidator.matchesJsonSchema(file))).
log().all();
jsonasmap.clear();
}
//body(JsonSchemaValidator.matchesJsonSchemaInClasspath("JsonSchemaValidator_DirectLogin_AWS.json").using(jsonSchemaFactory))
我尝试使用 jsonSchemaFactory 来执行此操作,但我没有得到关于将什么设置为草稿版本或从何处获取它的信息
我是新手,如果你觉得这个问题太简单了,请多多包涵
【问题讨论】:
-
在创建
JsonSchemaFactory时尝试指定要使用的架构草案。DRAFTV4是我使用的。 -
我用过它,但它不起作用,我认为它仅用于检查 json 响应是否为 DraftV4 版本
-
您可以发布您正在验证的架构吗?
-
这是我使用 JsonSchemaFactory JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder() 创建的代码。 setValidationConfiguration(ValidationConfiguration.newBuilder().setDefaultVersion(SchemaVersion.DRAFTV4).freeze()).freeze(); given().contentType(ContentType.JSON).body(jsonasmap).when().post("https://***d.execute-api.us-**-.amazonaws .com/some-api").then().assertThat().body(JsonSchemaValidator.matchesJsonSchema(file).using(jsonSchemaFactory)).log().all();如果您对改进它有任何建议,以便达到效果,请告诉
-
问题是我在模式中输入的内容是正确还是不正确,无论如何它都会显示通过
标签: json rest automation rest-assured rest-assured-jsonpath