从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<String, Object> additionalProperties = new HashMap<String, Object>();

/**

*

* @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<String, MyClass.StartDayOfWeek> constants = new HashMap<String, MyClass.StartDayOfWeek>();

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模式模块源代码中做了一些挖掘,发现发生的事情是Jackson使用“

.toString()”作为枚举类型的默认序列化方法,但是我需要做的是创建一行基于以下内容StartDayOfWeek.values()

"enum" : [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ]

有谁知道这是怎么做到的吗?

回答:

Storme的答案参考org.codehaus,它是jackson的旧版本。以下内容相似,但使用的是fastxml(新版本)。

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>

码:

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);

}

}

以上是 从POJO生成Json Schema 的全部内容, 来源链接: utcz.com/qa/415404.html

回到顶部