Micronaut JSON Schema
Generate a JSON Schema specification of your Java objects at compilation thanks to Micronaut JSON Schema.
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:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 21 or greater installed with
JAVA_HOMEconfigured appropriately
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.
-
Download and unzip the source
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:
annotationProcessor("io.micronaut.jsonschema:micronaut-json-schema-processor")implementation("io.micronaut.jsonschema:micronaut-json-schema-annotations")JSON Schema annotation
First create a class:
At compile time, a JSON Schema is generated. You can test it as follows:
Expose JSON Schema
We can expose the JSON Schema generated at compile time as a static resource with the following configuration:
micronaut.router.static-resources.jsonschema.mapping=/schemas/**
micronaut.router.static-resources.jsonschema.paths=classpath\:META-INF/schemasYou can test the JSON Schema specification is exposed:
Test
Last, let’s write a test that verifies the generated JSON Schema matches our expectations:
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:
testImplementation("org.skyscreamer:jsonassert:@jsonassertVersion@")Testing the Application
To run the tests:
./gradlew testThen 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). |