Java / Gradle

Micronaut JSON Schema

Generate a JSON Schema specification of your Java objects at compilation thanks to Micronaut JSON Schema.

Sergio del Amo
On this guide
In this section

Getting Started

This tutorial shows how to generate a JSON Schema of a Java class at compile-time using Micronaut JSON Schema. The example shows a JSON Schema, such as the one found in the Getting Started Tutorial on the JSON Schema website.

What you will need

To complete this guide, you will need the following:

Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide \
    --features=validation,json-schema \
    --build=gradle \
    --lang=java \
    --test=junit
Note
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

If you use Micronaut Launch, select Micronaut Application as application type and add validation, and json-schema features.

Note
If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application.

JSON Schema dependencies

The jsonschema feature adds the following dependencies:

build.gradle
annotationProcessor("io.micronaut.jsonschema:micronaut-json-schema-processor")
build.gradle
implementation("io.micronaut.jsonschema:micronaut-json-schema-annotations")

JSON Schema annotation

First create a class:

java/src/main/java/example/micronaut/Product.java

At compile time, a JSON Schema is generated. You can test it as follows:

java/src/test/java/example/micronaut/JsonSchemaGeneratedTest.java

Expose JSON Schema

We can expose the JSON Schema generated at compile time as a static resource with the following configuration:

src/main/resources/application.properties
micronaut.router.static-resources.jsonschema.mapping=/schemas/**
micronaut.router.static-resources.jsonschema.paths=classpath\:META-INF/schemas

You can test the JSON Schema specification is exposed:

java/src/test/java/example/micronaut/JsonSchemaExposedTest.java

Test

Last, let’s write a test that verifies the generated JSON Schema matches our expectations:

java/src/test/java/example/micronaut/ProductSchemaTest.java

JSON Assert

The previous test uses JSON Assert:

Write JSON tests as if you are comparing a string. Under the covers, JSONassert converts your string into a JSON object and compares the logical structure and data with the actual JSON.

You need to add the dependency to your test classpath:

build.gradle
testImplementation("org.skyscreamer:jsonassert:@jsonassertVersion@")

Testing the Application

To run the tests:

./gradlew test

Then open build/reports/tests/test/index.html in a browser to see the results.

Next Steps

Read more about Micronaut JSON Schema and JSON Schema.

License

Note
All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images).