Generate Json Schema from POJO with a twist

前端 未结 3 1355
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条回答
  • 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);
    }
    
    0 讨论(0)
  • 2020-12-28 19:02

    If someone comes here and wants to support the newest draft version in his code.

    Look here for your preferred language: https://json-schema.org/implementations.html

    0 讨论(0)
  • 2020-12-28 19:06

    Storme's answer references org.codehaus, which is an older version of jackson. The following is similar but uses fasterxml (newer version).

    Pom:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.7.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.7.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.7.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-jsonSchema</artifactId>
        <version>2.1.0</version>
    </dependency>
    

    Code:

    import ...TargetClass;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;
    import com.fasterxml.jackson.databind.jsonschema.JsonSchema;
    
    import java.io.IOException;
    
    public final class JsonSchemaGenerator {
    
        private JsonSchemaGenerator() { };
    
        public static void main(String[] args) throws IOException {
            System.out.println(JsonSchemaGenerator.getJsonSchema(TargetClass.class));
        }
    
        public static String getJsonSchema(Class clazz) throws IOException {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
            JsonSchema schema = mapper.generateJsonSchema(clazz);
            return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题