Generate Json Schema from POJO with a twist

前端 未结 3 1354
Happy的楠姐
Happy的楠姐 2020-12-28 18:53

What I have:

I\'m generating a JSON schema from a pojo. My code to generate the schema looks like so:

ObjectMapper mapper = new ObjectMapper();
Titl         


        
3条回答
  •  旧时难觅i
    2020-12-28 18:58

    It seems to not be possible using the instructions I found using databind. However I found another jackson module that seems to do the trick nicely. Oddly several of the objects are named the same.

    TLDR: 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);
    }
    

提交回复
热议问题