如何在Java中以编程方式从json模式生成json数据

如何在Java中以编程方式从json模式生成json数据,java,spring-boot,api,automation,qa,Java,Spring Boot,Api,Automation,Qa,我正在尝试为我的POST Api创建Body参数(JSON),这是一个JSON请求。我所拥有的只是JSON模式。我正试图列出一个不同的JSON测试数据列表,包括它的正流和负流 是否有使用Java编程生成/创建JSON数据的选项。我附加了一个小的Json模式(只是为了理解),但我的实际模式更复杂,有很多数组和嵌套的Json 我的Json模式: { "$schema": "http://json-schema.org/draft-07/schema", "$id": "http://example.

我正在尝试为我的POST Api创建Body参数(JSON),这是一个JSON请求。我所拥有的只是JSON模式。我正试图列出一个不同的JSON测试数据列表,包括它的正流和负流

是否有使用Java编程生成/创建JSON数据的选项。我附加了一个小的Json模式(只是为了理解),但我的实际模式更复杂,有很多数组和嵌套的Json

我的Json模式:

{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"title": "The Root Schema",
"description": "The root schema comprises the entire JSON document.",
"required": [
    "FirstName",
    "LastName",
    "Age",
    "Interest"
],
"properties": {
    "FirstName": {
        "$id": "#/properties/FirstName",
        "type": "string",
        "title": "The Firstname Schema",
        "description": "An explanation about the purpose of this instance.",
        "default": "",
        "examples": [
            "Vijay"
        ]
    },
    "LastName": {
        "$id": "#/properties/LastName",
        "type": "string",
        "title": "The Lastname Schema",
        "description": "An explanation about the purpose of this instance.",
        "default": "",
        "examples": [
            "Karthik"
        ]
    },
    "Age": {
        "$id": "#/properties/Age",
        "type": "integer",
        "title": "The Age Schema",
        "description": "An explanation about the purpose of this instance.",
        "default": 0,
        "examples": [
            30
        ]
    },
    "Interest": {
        "$id": "#/properties/Interest",
        "type": "array",
        "title": "The Interest Schema",
        "description": "An explanation about the purpose of this instance.",
        "default": [],
        "items": {
            "$id": "#/properties/Interest/items",
            "type": "string",
            "title": "The Items Schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "Food",
                "movie",
                "Learning",
                "VideoGames"
            ]
        }
    }
}
}
在此处输入代码

我的测试数据如下所示:

 {
"FirstName":"Vivi",
"LastName":"Karrri",
"Age":30,
"Interest":["Food","movie","Learning","VideoGames"]
}
有什么建议我们怎样才能做到这一点?
注意:我使用的是Springboot,我有完整的请求对象POJO,您可以生成假的java对象,然后将它们映射到JSON

POJO

如果您已经有了与模式匹配的POJO,那么我们可以跳过这一步。 如果否,例如,可以使用此库从架构生成POJO:

假对象

使用特殊的库可以生成包含伪数据的对象,其中一些列在此处:

生成JSON

这非常简单,可以通过Jackson完成:

ObjectMapper ObjectMapper=new ObjectMapper();
ObjectWriter prettyPrinter=objectMapper.writerWithDefaultPrettyPrinter();
字符串json=prettyPrinter.writeValueAsString(yourFakeObject);

您可以生成伪java对象,然后将它们映射到JSON

POJO

如果您已经有了与模式匹配的POJO,那么我们可以跳过这一步。 如果否,例如,可以使用此库从架构生成POJO:

假对象

使用特殊的库可以生成包含伪数据的对象,其中一些列在此处:

生成JSON

这非常简单,可以通过Jackson完成:

ObjectMapper ObjectMapper=new ObjectMapper();
ObjectWriter prettyPrinter=objectMapper.writerWithDefaultPrettyPrinter();
字符串json=prettyPrinter.writeValueAsString(yourFakeObject);

如果您有json模式,那么您可以直接从中生成json消息示例


如果您有json模式,那么您可以直接从中生成一个json消息示例


我已经为伪造对象编写了自己的库:。它背后的思想是使用“有意义”的数据填充对象,并基于给定的业务约束列表。@llya Lysenko是否可以从Json模式生成Json/string,而无需生成POJO,或任何可以帮助我从Json模式生成Json的java库我已经编写了自己的伪造对象库:。其背后的思想是使用“有意义”的数据填充对象,并基于给定的业务约束列表。@llya Lysenko是否可以从Json模式生成Json/string,而无需生成POJO,或者任何可以帮助我从json模式生成json的java库Akarthikeyan你得到这个问题的解决方案了吗,我也陷入了同样类型的问题中,如果你找到了解决方案,请让我知道..Karthikeyan你得到了这个问题的解决方案了吗,我也陷入了同样类型的问题中,如果你找到了解决办法,请告诉我。。