Generate JSON Schema from Java Class

In this tutorial we are going to see about how to generate JSON Schema from Java Class using Jackson 1.x.


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

Jackson Maven Dependency

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

JSONSchemaGenerator.java

package com.ehowtonow.jackson;

import java.io.IOException;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig.Feature;
import org.codehaus.jackson.schema.JsonSchema;

public class JSONSchemaGenerator {

	public static void main(String[] args) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.configure(Feature.WRITE_ENUMS_USING_TO_STRING, true);
		JsonSchema schema;
		try {
			schema = mapper.generateJsonSchema(Student.class);
			String jsonSchema = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
			System.out.println(jsonSchema);
		} catch (JsonMappingException e) {
			
			e.printStackTrace();
		} catch (JsonGenerationException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}

	    
	}

}

Student.java

package com.ehowtonow.jackson;

import java.util.Date;

public class Student {

	private String name;
	private String standard;
	private int age;
	private  Date date;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getStandard() {
		return standard;
	}
	public void setStandard(String standard) {
		this.standard = standard;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
		
}

JSON Schema Output

{
  "type" : "object",
  "properties" : {
    "name" : {
      "type" : "string"
    },
    "standard" : {
      "type" : "string"
    },
    "age" : {
      "type" : "integer"
    },
    "date" : {
      "type" : "number"
    }
  }
}

Ask your questions in eHowToNow Forum

To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

Posts You may Like

  1. How to delete file using Java
  2. Java Mail API – Introduction
  3. Java Best Practice – File Organization
  4. Java 7 Try statement with resources
  5. How to Create File using Java
  6. How to replace the element from List
  7. Java Tutorial – Introduction to Java Virtual Machine (JVM)
  8. JAX-RS to Produce JSON using Jackson – Jersey 1.x
  9. How to validate Username using Java Regular Expression
  10. How to validate IPv4 Address with Port using Java Regular Expression
  11. Load and Parse HTML from url using JSoup
  12. How to delete directory using Java
  13. Convert JSON to Map using Jackson
  14. How to Read file using FileInputStream
  15. How to configure Maven to use Java 8

Be the first to comment

Leave a Reply

Your email address will not be published.


*