jackson - Generate JSON schema from java class - Stack Overflow
I ran into a need to do this myself, but needed to get the latest schema spec (v4 as of this post). My solution is the first answer at the link below: Generate Json Schema from POJO with a twist
Use objects from the org.codehaus.jackson.map package rather than the com.fasterxml.jackson.databind package. If you're following the instructions on this page then you're doing it wrong. Just use the jackson-mapper module instead.
Here's the code for future googlers:
private static String getJsonSchema(Class clazz) throws IOException { org.codehaus.jackson.map.ObjectMapper mapper = new ObjectMapper(); //There are other configuration options you can set. This is the one I needed. mapper.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, true); JsonSchema schema = mapper.generateJsonSchema(clazz); return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema); }
Read full article from jackson - Generate JSON schema from java class - Stack Overflow
No comments:
Post a Comment