简体   繁体   中英

How to create a bean dynamically in spring boot using a json schema in runtime

I want to create a java bean that is entirely dynamic. First, we need to store the schema of the object and also need to store what properties we can fetch on runtime. If any modification is done in real-time this bean should be able to adapt to the changes.

  1. Store schema properties either in properties file/database
    "$schema": "http://json-schema.org/draft-06/schema#",
    "$ref": "#/definitions/Welcome3",
    "definitions": {
        "Welcome3": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "first": {
                    "type": "string"
                },
                "last": {
                    "type": "string"
                },
                "email": {
                    "type": "string"
                },
                "password": {
                    "type": "string"
                },
                "address": {
                    "$ref": "#/definitions/Address"
                }
            },
            "required": [
                "address",
                "email",
                "first",
                "last",
                "password"
            ],
            "title": "Welcome3"
        },
        "Address": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "street": {
                    "type": "string"
                },
                "district": {
                    "type": "string"
                },
                "pincode": {
                    "type": "string"
                }
            },
            "required": [
                "district",
                "pincode",
                "street"
            ],
            "title": "Address"
        }
    }
}

  1. Store Bean Fetching properties either in properties file/database

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "$ref": "#/definitions/Welcome5",
    "definitions": {
        "Welcome5": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "first": {
                    "type": "string"
                },
                "last": {
                    "type": "string"
                },
                "email": {
                    "type": "string"
                },
                "pincode": {
                    "type": "string"
                }
            },
            "required": [
                "email",
                "first",
                "last",
                "pincode"
            ],
            "title": "Welcome5"
        }
    }
}


After setting up Schema properties and Fetching properties. I want to set up a test class and construct bean from schema properties and be able to print the values of properties mentioned in Fetching properties.

So, first of all, I would say whoever has worked with JAVA and JSON even for once in their lifetime know this is a common desire which we have. But unfortunately, JAVA doesn't work in that manner.

For JAVA to handle any object it must know its datastructre/datatype be it a JSON response or a DB record or even if you are storing a number it has so many data types to handle that and you have to specify that at compile time.

So this is not possible as far as I know but I won't leave you in vain. My suggestion to handle this kind of situation would be to use the GSON library

You can add it by following ways

For Gradle projects

dependencies {
    implementation 'com.google.code.gson:gson:2.8.8'
}

For Maven projects

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.8</version>
</dependency>

Or you can simply add JAR file from here

you can store a whole JSON file under a gson a variable as long as they follow JSON structre.

And you can navigate to the required field and use .get(<field_name>) to get that particular data. recommend you to check it out and play with it you will get a good idea on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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