Skip to content

Instantly share code, notes, and snippets.

@mpellegrini
Last active March 18, 2020 13:03
Show Gist options
  • Save mpellegrini/7571845 to your computer and use it in GitHub Desktop.
Save mpellegrini/7571845 to your computer and use it in GitHub Desktop.
Generate JSON Schema using Jackson
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonSchemaGenerator generator = new JsonSchemaGenerator(mapper);
JsonSchema jsonSchema = generator.generateSchema(Subscription.class);
StringWriter json = new StringWriter();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.writeValue(json, jsonSchema);
System.out.println(json.toString());
}
/*
** Needs the following dependency **
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jsonSchema</artifactId>
<version>2.3.0</version>
</dependency>
*/
@macvek
Copy link

macvek commented Apr 17, 2018

You have to represent your input.json as a class with proper annotations (here is an example with a Subscription.class) and start this program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment