Posted In: Jackson 2X, JSON

Generate Json Schema from POJO – Jackson 2X

 

1. Example is using Address POJO with multiple fields.

package com.example;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang3.builder.ToStringBuilder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
@JsonPropertyOrder({
    "addressID",
    "country",
    "state",
    "pinCode"
})
public class Address2 {
    @JsonProperty("addressID")
    private String addressID;
    @JsonProperty("country")
    private String country;
    @JsonProperty("state")
    private String state;
    @JsonProperty("pinCode")
    private String pinCode;
    @JsonProperty("addressID")    
    public String getAddressID() {
        return addressID;
    }
    @JsonProperty("addressID")
    public void setAddressID(String addressID) {
        this.addressID = addressID;
    }
    @JsonProperty("country")
    public String getCountry() {
        return country;
    }
    @JsonProperty("country")
    public void setCountry(String country) {
        this.country = country;
    }
    @JsonProperty("state")
    public String getState() {
        return state;
    }
    @JsonProperty("state")
    public void setState(String state) {
        this.state = state;
    }
    @JsonProperty("pinCode")
    public String getPinCode() {
        return pinCode;
    }
    @JsonProperty("pinCode")
    public void setPinCode(String pinCode) {
        this.pinCode = pinCode;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

 

2. Use com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator class to get schema from POJO class

 

Maven dependency for jackson-module-jsonSchema. jackson-module-jsonSchema
<dependency>
	<groupId>com.fasterxml.jackson.module</groupId>
	<artifactId>jackson-module-jsonSchema</artifactId>
	<version>2.8.6</version>
</dependency>

 

package com.example;

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

//Generate Json Schema from POJO
public class Main6 {
	public static void main(String a[]) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(
			        mapper);
			JsonSchema schema = schemaGen
			        .generateSchema(Address2.class);
			System.out.println(
			        mapper.writerWithDefaultPrettyPrinter()
			                .writeValueAsString(schema));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

3. Schema generated

{
  "type" : "object",
  "id" : "urn:jsonschema:com:example:Address2",
  "properties" : {
    "addressID" : {
      "type" : "string"
    },
    "country" : {
      "type" : "string"
    },
    "state" : {
      "type" : "string"
    },
    "pinCode" : {
      "type" : "string"
    }
  }
}

 
 

Tags: Tags: ,

by , on February 21st, 2017