简体   繁体   English

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

[英]How to generate json data from Json Schema Programmatically in Java

I am trying to create Body-parameter(JSON) for my POST Api , which is a JSON request .我正在尝试为我的 POST Api 创建 Body-parameter(JSON) ,这是一个 JSON 请求。 All I have is the JSON Schema .我所拥有的只是 JSON Schema 。 I am trying to come up with a list of different JSON test data covering Positive and negative flows for it .我正在尝试为它提出涵盖正流和负流的不同 JSON 测试数据的列表。

Is there any option to generate / create the JSON data programmatic using Java ?是否有任何选项可以使用 Java 以编程方式生成/创建 JSON 数据? . . I have attached a small Json schema (just for understanding purpose) but my actual schema is more complicated with lot of Array's and Nested Json's .我附上了一个小的 Json 模式(只是为了理解目的),但我的实际模式更复杂,有很多 Array 和 Nested Json 的 .

My Json Schema :我的 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 enter code here

My TestData looks like :我的 TestData 看起来像:

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

Any suggestions how can we achive this ?任何建议我们如何实现这一目标? Note : I am using Springboot and I have complete POJO for the request object注意:我正在使用 Springboot 并且我有完整的请求对象的 POJO

You can generate fake java-objects and then map them to JSON.您可以生成伪造的 java 对象,然后将它们映射到 JSON。

POJOs POJO

If you already have the POJOs matching the schema, then we can skip this step.如果您已经有与模式匹配的 POJO,那么我们可以跳过这一步。 If no, to generate a POJO from the schema, for example, can be used this library: jsonschema2pojo .如果没有,例如从模式生成 POJO,可以使用这个库: jsonschema2pojo

Fake objects假货

Generating of objects with fake data can be done with a special library, some of them are listed here:可以用一个特殊的库来生成带有假数据的对象,这里列出了其中的一些:

Generating JSON生成 JSON

It's prettry simple and can be done with Jackson:这很简单,可以用 Jackson 完成:

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

If you have json schema then you can directly generate a sample JSON message from it.如果您有 json 模式,那么您可以直接从中生成示例 JSON 消息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM