【问题标题】:How to generate json data from Json Schema Programmatically in Java如何在 Java 中以编程方式从 Json Schema 生成 json 数据
【发布时间】:2020-06-19 16:20:27
【问题描述】:

我正在尝试为我的 POST Api 创建 Body-parameter(JSON),这是一个 JSON 请求。我只有 JSON Schema 。我正在尝试列出不同的 JSON 测试数据,涵盖正流和负流。

是否有任何选项可以使用 Java 以编程方式生成/创建 JSON 数据? .我附上了一个小的 Json 模式(只是为了理解目的),但我的实际模式更复杂,有很多 Array's 和 Nested Json's 。

我的 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"
            ]
        }
    }
}

}enter code here

我的 TestData 看起来像:

 {
"FirstName":"Vivi",
"LastName":"Karrri",
"Age":30,
"Interest":["Food","movie","Learning","VideoGames"]
}

任何建议我们如何才能实现这一目标? 注意:我正在使用 Springboot,并且请求对象有完整的 POJO

【问题讨论】:

  • Karthikeyan 你得到这个问题的解决方案了吗,我也遇到了同样类型的问题,如果你找到了解决方案,请告诉我..

标签: java spring-boot api automation qa


【解决方案1】:

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

POJO

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

假对象

可以使用一个特殊的库来生成带有假数据的对象,其中一些列在这里:

生成 JSON

这很简单,可以用 Jackson 完成:

ObjectMapper objectMapper = new ObjectMapper();
ObjectWriter prettyPrinter = objectMapper.writerWithDefaultPrettyPrinter();
String json = prettyPrinter.writeValueAsString(yourFakeObject);

【讨论】:

  • 我已经为伪造对象编写了自己的库:mockneat.com/tutorial/#composing-objects。它背后的想法是用“有意义”的数据填充对象,并基于给定的业务约束列表。
  • @llya Lysenko 是否可以从 Json 模式生成 Json/ 字符串而不生成 POJO 或任何可以帮助我从 json 模式生成 json 的 java 库
【解决方案2】:

如果您有 json 架构,那么您可以直接从中生成示例 JSON 消息。

https://github.com/jignesh-dhua/json-generator

【讨论】:

    猜你喜欢
    • 2015-07-29
    • 2022-01-17
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    相关资源
    最近更新 更多