Java 删除“;id";使用jackson从POJO创建JSON模式时从JSON模式

Java 删除“;id";使用jackson从POJO创建JSON模式时从JSON模式,java,json,jackson,Java,Json,Jackson,如何删除id字段(“id”:“urn:jsonschema:org:gradle:Person”) 从使用Jackson创建的JSON模式 生成的模式 { "type" : "object", "id" : "urn:jsonschema:org:gradle:Person", "properties" : { "name" : { "type" : "string" } } } import com.fasterxml.jackson.annota

如何删除id字段(“id”:“urn:jsonschema:org:gradle:Person”) 从使用Jackson创建的JSON模式

生成的模式

{
  "type" : "object",
  "id" : "urn:jsonschema:org:gradle:Person",
  "properties" : {
    "name" : {
      "type" : "string"
    }
  }
}
import com.fasterxml.jackson.annotation.JsonProperty;

public class Person {

    @JsonProperty("name")
    private String name;

} 
import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;


public final class GetJsonSchema {
    public static String getJsonSchema2(Class clazz) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(mapper);
        JsonSchema jsonSchema = jsonSchemaGenerator.generateSchema(clazz);
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
    }
}
对于POJO类(Person.class)

{
  "type" : "object",
  "id" : "urn:jsonschema:org:gradle:Person",
  "properties" : {
    "name" : {
      "type" : "string"
    }
  }
}
import com.fasterxml.jackson.annotation.JsonProperty;

public class Person {

    @JsonProperty("name")
    private String name;

} 
import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;


public final class GetJsonSchema {
    public static String getJsonSchema2(Class clazz) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(mapper);
        JsonSchema jsonSchema = jsonSchemaGenerator.generateSchema(clazz);
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
    }
}
使用JSON模式生成器

{
  "type" : "object",
  "id" : "urn:jsonschema:org:gradle:Person",
  "properties" : {
    "name" : {
      "type" : "string"
    }
  }
}
import com.fasterxml.jackson.annotation.JsonProperty;

public class Person {

    @JsonProperty("name")
    private String name;

} 
import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;


public final class GetJsonSchema {
    public static String getJsonSchema2(Class clazz) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(mapper);
        JsonSchema jsonSchema = jsonSchemaGenerator.generateSchema(clazz);
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
    }
}
调用类似于

System.out.println(JsonSchema.Create(Person.class));

只需将id设置为
null
。 例如:


只需将id设置为
null
。 例如:


正如sachin所说,
jsonSchema.setId(null)
是实现目标的好方法。但是Venkat的观点是正确的,复杂类型仍然会有id

删除它们的一种方法是使用自定义的
SchemaFactoryWrapper
,它将实例化自己的
visitorContext
,而visitorContext将拒绝提供URN。但是,重要的是要注意,如果一个类型引用自身(例如,可能有子状态对象的状态对象),则这将不起作用

例如:

private static class IgnoreURNSchemaFactoryWrapper extends SchemaFactoryWrapper {
    public IgnoreURNSchemaFactoryWrapper() {
        this(null, new WrapperFactory());
    }

    public IgnoreURNSchemaFactoryWrapper(SerializerProvider p) {
        this(p, new WrapperFactory());
    }

    protected IgnoreURNSchemaFactoryWrapper(WrapperFactory wrapperFactory) {
        this(null, wrapperFactory);
    }

    public IgnoreURNSchemaFactoryWrapper(SerializerProvider p, WrapperFactory wrapperFactory) {
        super(p, wrapperFactory);
        visitorContext = new VisitorContext() {
            public String javaTypeToUrn(JavaType jt) {
                return null;
            }
        };
    }
}

private static final String printSchema(Class c) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        IgnoreURNSchemaFactoryWrapper visitor = new IgnoreURNSchemaFactoryWrapper();
        mapper.acceptJsonFormatVisitor(c, visitor);
        JsonSchema schema = visitor.finalSchema();
        schema.setId(null);
        ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
        String asString = writer.writeValueAsString(schema);
        return asString;
    } catch(Exception e) {
        e.printStackTrace();
        return null;
    }
}

正如sachin所说,
jsonSchema.setId(null)
是实现目标的好方法。但是Venkat的观点是正确的,复杂类型仍然会有id

删除它们的一种方法是使用自定义的
SchemaFactoryWrapper
,它将实例化自己的
visitorContext
,而visitorContext将拒绝提供URN。但是,重要的是要注意,如果一个类型引用自身(例如,可能有子状态对象的状态对象),则这将不起作用

例如:

private static class IgnoreURNSchemaFactoryWrapper extends SchemaFactoryWrapper {
    public IgnoreURNSchemaFactoryWrapper() {
        this(null, new WrapperFactory());
    }

    public IgnoreURNSchemaFactoryWrapper(SerializerProvider p) {
        this(p, new WrapperFactory());
    }

    protected IgnoreURNSchemaFactoryWrapper(WrapperFactory wrapperFactory) {
        this(null, wrapperFactory);
    }

    public IgnoreURNSchemaFactoryWrapper(SerializerProvider p, WrapperFactory wrapperFactory) {
        super(p, wrapperFactory);
        visitorContext = new VisitorContext() {
            public String javaTypeToUrn(JavaType jt) {
                return null;
            }
        };
    }
}

private static final String printSchema(Class c) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        IgnoreURNSchemaFactoryWrapper visitor = new IgnoreURNSchemaFactoryWrapper();
        mapper.acceptJsonFormatVisitor(c, visitor);
        JsonSchema schema = visitor.finalSchema();
        schema.setId(null);
        ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
        String asString = writer.writeValueAsString(schema);
        return asString;
    } catch(Exception e) {
        e.printStackTrace();
        return null;
    }
}

当POJO是简单的时候,这很好,如果POJO是复杂的内部对象呢?当POJO是简单的时候,这很好,如果POJO是复杂的内部对象呢?