Jackson: generate schemas with references

前端 未结 3 949
忘掉有多难
忘掉有多难 2021-01-18 01:20

When using Jackson\'s JSON schema module, instead of serializing the complete graph I\'d like to stop whenever one of my model classes is encountered, and use the class nam

3条回答
  •  终归单人心
    2021-01-18 01:44

    You can try and use following code -

        ObjectMapper MAPPER = new ObjectMapper();
        SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
    
        JsonSchemaGenerator generator = new JsonSchemaGenerator(MAPPER);
    
        JsonSchema jsonSchema = generator.generateSchema(MyBean.class);
    
        System.out.println(MAPPER.writeValueAsString(jsonSchema));
    

    But your expected output is not valid, it won't say $ref, unless it has specified the schema for "Animals" at least once.

    {
        "type": "object",
        "id": "urn:jsonschema:com:tibco:tea:agent:Zoo",
        "properties": {
            "animals": {
                "type": "array",
                "items": {
                    "type": "object",
                    "id": "urn:jsonschema:com:tibco:tea:agent:Animal",
                    "properties": {
                        "species": {
                            "type": "string"
                        }
                    }
                }
            },
            "name": {
                "type": "string"
            }
        }
    }
    

提交回复
热议问题