Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从响应中验证特定json对象的json模式_Java_Json_Jsonschema_Rest Assured_Rest Assured Jsonpath - Fatal编程技术网

Java 从响应中验证特定json对象的json模式

Java 从响应中验证特定json对象的json模式,java,json,jsonschema,rest-assured,rest-assured-jsonpath,Java,Json,Jsonschema,Rest Assured,Rest Assured Jsonpath,我有一个类似这样的json响应(响应采用com.jayway.restassured.response.response格式) 因此,我只需要验证views对象的json模式,而不需要验证整个json。为此 我已经为视图仅对象模式1创建了一个json模式 schema1.json { "type": "array", "items": { "id": "view.json", "type": "object", "propertie

我有一个类似这样的json响应(响应采用
com.jayway.restassured.response.response
格式)

因此,我只需要验证views对象的json模式,而不需要验证整个json。为此 我已经为视图仅对象模式1创建了一个json模式

schema1.json

{
    "type": "array",
    "items": {
        "id": "view.json",
        "type": "object",
        "properties": {
            "name": {
                "type": "string"
            },
            "displayOrder": {
                "type": "integer",
                "minimum": 1
            },
            "groups": {
                "type": "array"             
            },
            "localizedName": {
                "type": "object",
                "properties": {
                    "ENG": {
                        "type": "string",
                        "description": "the name of the view in english"
                    },
                    "ESP": {
                        "type": "string",
                        "description": "the name of the view in spanish"
                    }
                }
            }
        }
    }
}
如何执行特定json对象(来自josn响应的视图对象)的模式验证

代码


可以指定在将数组作为其属性保存的对象上允许附加属性。以下是整个响应json对象的模式:

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "array",
    "items": {
        "type": "object",
        "required": ["views"],
        "additionalProperties": true,
        "properties": {
            "views": {
                "type": "array",
                "items": {
                    "id": "view.json",
                  ...
        }
    }
}

这意味着只需要修改josn模式,对吗?我们不需要在代码中做任何修改。请你澄清一下好吗。仅修改json模式。答案中的模式是根据问题中的回答量身定制的(这意味着响应是一个包含对象的数组,
视图
是一个项目的属性),您可能需要使用模式来适应结构OK。此外,我还可以传递整个响应体来验证特定的视图对象。无需仅提取视图对象响应..对吗?除此之外..请推荐一个好的模式生成器工具。
Response response = RestAssured.given().when().get(getURL);
 ValidatableResponse valResponse = response.then().contentType(ContentType.JSON);
  valResponse.body(schema1.json("schema1.json")).assertThat();
{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "array",
    "items": {
        "type": "object",
        "required": ["views"],
        "additionalProperties": true,
        "properties": {
            "views": {
                "type": "array",
                "items": {
                    "id": "view.json",
                  ...
        }
    }
}