Jackson JsonSchemaGenerator - How to get schema as string

I'm sure I'm just tight here.

I want to take an object schema and turn it into a string representation.

Same, but this returns null:

JsonSchemaGenerator generator = new JsonSchemaGenerator(MAPPER);
JsonSchema jsonSchema = generator.generateSchema(Get.class);
System.out.println("jsonSchema: " + jsonSchema.asObjectSchema().asStringSchema());

      

This is used by com.fasterxml.jackson.module.jsonSchema.JsonSchema found at https://github.com/FasterXML/jackson-module-jsonSchema/wiki

+3


source to share


2 answers


you can achieve this by running:



ObjectMapper mapper = new ObjectMapper();
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
mapper.acceptJsonFormatVisitor(mapper.constructType(YOURCLASS.class), visitor);
JsonSchema jsonSchema = visitor.finalSchema();
String schemaString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);

      

+6


source


Easily done:

m.writeValueAsString(jsonSchema);

      



Essentially using Jackson to marshal a schema object to JSON.

0


source







All Articles