从POJO生成Json Schema
是)我有的:
我正在从pojo生成JSON模式。 我生成模式的代码如下所示:
ObjectMapper mapper = new ObjectMapper(); TitleSchemaFactoryWrapper visitor = new TitleSchemaFactoryWrapper(); mapper.acceptJsonFormatVisitor(clazz, visitor); JsonSchema schema = visitor.finalSchema(); schemas.put(clazz, mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema));
我通过上面的代码生成了几个模式。 其中一个pojos有一个内部嵌入枚举来限制可能的值,如下所示:
public class MyClass { @JsonProperty("name") private String name; @JsonProperty("startDayOfWeek") private MyClass.StartDayOfWeek startDayOfWeek; /** * The ID of a timezone returned by the timezones route. * */ @JsonProperty("timezone") private String timezone; @JsonIgnore private Map additionalProperties = new HashMap(); /** * * @return * The startDayOfWeek */ @JsonProperty("startDayOfWeek") public MyClass.StartDayOfWeek getStartDayOfWeek() { return startDayOfWeek; } /** * * @param startDayOfWeek * The startDayOfWeek */ @JsonProperty("startDayOfWeek") public void setStartDayOfWeek(MyClass.StartDayOfWeek startDayOfWeek) { this.startDayOfWeek = startDayOfWeek; } public static enum StartDayOfWeek { MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY("Friday"), SATURDAY("Saturday"), SUNDAY("Sunday"); private final String value; private static Map constants = new HashMap(); static { for (MyClass.StartDayOfWeek c: values()) { constants.put(c.value, c); } } private StartDayOfWeek(String value) { this.value = value; } @JsonValue @Override public String toString() { return this.value; } @JsonCreator public static MyClass.StartDayOfWeek fromValue(String value) { MyClass.StartDayOfWeek constant = constants.get(value); if (constant == null) { throw new IllegalArgumentException(value); } else { return constant; } } } }
上面的代码应该限制传递给“星期一”,“星期二”,“星期三”等的JSON数据中可能的String值。
当我在相关代码上运行模式生成器时,我希望得到类似以下模式的东西:
{ "type" : "object", "javaType" : "my.package.MyClass", "properties": { "startDayOfWeek" : { "type" : "string", "enum" : [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ] } } }
但相反,我得到了这个:
{ "type" : "object", "id" : "urn:jsonschema:my:package:MyClass", "title" : "Lmy/package/MyClass;", "properties" : { "startDayOfWeek" : { "type" : "string" } } }
我已经完成了对Jackson Schema Module源代码的一些挖掘,并发现正在发生的事情是Jackson使用“.toString()”作为枚举类型的默认序列化方法,但我需要它做的是创建一条线看起来像这样基于StartDayOfWeek.values()
:
"enum" : [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ]
有谁知道这是怎么做到的吗?
Storme的回答引用了org.codehaus
,它是jackson的旧版本。 以下是类似但使用fasterxml(较新版本)。
双响炮:
com.fasterxml.jackson.core jackson-core 2.7.1 com.fasterxml.jackson.core jackson-databind 2.7.1 com.fasterxml.jackson.core jackson-annotations 2.7.1 com.fasterxml.jackson.module jackson-module-jsonSchema 2.1.0
码:
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); } }
似乎无法使用我在databind中找到的指令。 然而,我发现另一个jackson模块似乎很好地完成了这个伎俩。 奇怪的是,有几个对象被命名为相同。
TLDR:使用org.codehaus.jackson.map
包中的对象而不是com.fasterxml.jackson.databind
包。 如果你按照本页面上的说明进行操作,那么你做错了。 只需使用jackson-mapper模块即可。
以下是未来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); }