Java to json schema maven example

Java to json schema maven example

Java to json schema maven example:
YAML and JSON are simple and nice format for structured data and easier for human to read and write than XML. But there have been no schema for YAML such as RelaxNG or DTD. Below is an example that convert java object to json schema. Check post for java object to yaml examplejava to yaml

  • Create java maven project; maven
  • Add below dependencies to your project:
  • 1
    2
    3
    4
    5
    6
    7
    
    <dependencies>
    	<dependency>
    			<groupId>com.fasterxml.jackson.module</groupId>
    			<artifactId>jackson-module-jsonSchema</artifactId>
    			<version>2.4.1</version>
    		</dependency>
    </dependencies>
  • Create class that you want to convert to json schema. In example I have used Object class, replace it with your own class-
  • 1
    2
    3
    4
    5
    6
    7
    
     public static void  jsonSchemaGenerator() throws JsonProcessingException{
          ObjectMapper mapper = new ObjectMapper();
          SchemaFactoryWrapper actionRes = new SchemaFactoryWrapper();
          mapper.acceptJsonFormatVisitor(Object.class, actionRes);
          JsonSchema schema = actionRes.finalSchema();
          System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema));
        }

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.