4. Usage
4.1. Maven (use in unit tests)
Add the following dependency to your pom.xml
:
<dependency>
<groupId>org.alexmond</groupId>
<artifactId>spring-boot-config-json-schema-starter</artifactId>
<version>0.0.5</version>
<scope>test</scope>
</dependency>
SampleJsonSchemaGeneratorTests.java
@SpringBootTest
@Slf4j
class SampleJsonSchemaGeneratorTests {
@Autowired
private JsonSchemaService jsonSchemaService;
@Test
void generateJsonSchema() throws Exception {
String jsonConfigSchema;
jsonConfigSchema = jsonSchemaService.generateFullSchema();
ObjectMapper jsonMapper = new ObjectMapper();
ObjectWriter jsonWriter = jsonMapper.writer(new DefaultPrettyPrinter());
log.info("Writing json schema");
jsonWriter.writeValue(Paths.get("sample-schema.json").toFile(),
jsonMapper.readTree(jsonConfigSchema));
ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
ObjectWriter yamlWriter = yamlMapper.writer(new DefaultPrettyPrinter());
log.info("Writing yaml schema");
yamlWriter.writeValue(Paths.get("sample-schema.yaml").toFile(),
jsonMapper.readTree(jsonConfigSchema));
}
}
4.2. Using as REST API Endpoint
To expose the JSON schema via a REST endpoint (similar to Swagger API docs), first add the following dependency to your pom.xml
:
<dependency>
<groupId>org.alexmond</groupId>
<artifactId>spring-boot-config-json-schema-starter</artifactId>
<version>0.0.5</version>
</dependency>
GenerateJsonSchema.java
@RestController
@Slf4j
public class GenerateJsonSchema {
@Autowired
private JsonSchemaService jsonSchemaService;
@GetMapping("/config-schema")
public String getConfigSchema() throws Exception {
return jsonSchemaService.generateFullSchema();
}
}