Thursday 8 April 2021

Generate json schema from a java class

Using 'generateSchema' method of JsonSchemaGenerator class, we can generate a json schema.

 

Example

public static String getJsonSchema(Class<?> type) throws JsonProcessingException {
	ObjectMapper jacksonObjectMapper = new ObjectMapper();
	JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(jacksonObjectMapper);
	JsonSchema schema = schemaGen.generateSchema(type);
	String schemaString = jacksonObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
	return schemaString;
}

 

Find the below working application.

 

Define Address and Employee model classes.

 

Address.java

 

package com.sample.app.model;

public class Address {

	private String street;
	private String city;
	private String country;

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

}

 

Employee.java

package com.sample.app.model;

import java.util.List;

public class Employee {

	private int id;
	private String firstName;
	private String lastName;
	private Address address;
	private List<String> hobbies;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public List<String> getHobbies() {
		return hobbies;
	}

	public void setHobbies(List<String> hobbies) {
		this.hobbies = hobbies;
	}

}

 

Define JsonSchemaUtil class.

 

JsonSchemaUtil.java

package com.sample.app.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;

public class JsonSchemaUtil {

	public static String getJsonSchema(Class<?> type) throws JsonProcessingException {
		ObjectMapper jacksonObjectMapper = new ObjectMapper();
		JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(jacksonObjectMapper);
		JsonSchema schema = schemaGen.generateSchema(type);
		String schemaString = jacksonObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
		return schemaString;
	}
}

 

Define SchemaFromClass.

 

SchemaFromClass.java

package com.sample.app;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sample.app.model.Employee;
import com.sample.app.util.JsonSchemaUtil;

public class SchemaFromClass {

	public static void main(String args[]) throws JsonProcessingException {

		String jsonSchema = JsonSchemaUtil.getJsonSchema(Employee.class);

		System.out.println(jsonSchema);
	}

}

 

Output

{
  "type" : "object",
  "id" : "urn:jsonschema:com:sample:app:model:Employee",
  "properties" : {
    "id" : {
      "type" : "integer"
    },
    "firstName" : {
      "type" : "string"
    },
    "lastName" : {
      "type" : "string"
    },
    "address" : {
      "type" : "object",
      "id" : "urn:jsonschema:com:sample:app:model:Address",
      "properties" : {
        "street" : {
          "type" : "string"
        },
        "city" : {
          "type" : "string"
        },
        "country" : {
          "type" : "string"
        }
      }
    },
    "hobbies" : {
      "type" : "array",
      "items" : {
        "type" : "string"
      }
    }
  }
}

 

 

 

 

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment