如何为 JAXB/Moxy 带注释的 POJO class 生成 JSONSchema
How to generate the JSONSchema for the JAXB/Moxy annotated POJO class
我知道有很多问题与使用 Jackson
从 JAXB
注释 classes 生成 JSONSchema
有关,但我找不到任何示例使用 MOXY
注释生成的 JSONSchema class.
我只想知道如何为我的 MOXY
注释 class 生成 JSONSchema
?截至目前,当我为 JAXB/Moxy
注释 class 生成 JSONSchema
时,我只得到一个字段:
{
"type" : "any"
}
以下是我的 class,我想使用 Jackson
和 JAXB/Moxy
注释为其生成 JSONSchema
:(如您所见,我有 name
和 age
作为必填字段,但它们不会显示在生成的 JSONSchema
)
中
@XmlRootElement(name = "Customer")
@XmlType(name = "Customer", propOrder = {"name", "age", "userExtensions"})
@XmlAccessorType(XmlAccessType.FIELD)
@NoArgsConstructor
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
public class Customer extends Person {
@XmlElement(name = "name",required = true)
private String name;
@XmlElement(name = "name",required = true)
private String age;
@JsonSerialize(using = CustomExtensionsSerializer.class)
@XmlJavaTypeAdapter(TestAdapter.class)
@XmlPath(".")
@JsonValue
private Map<String, Object> userExtensions = new HashMap<>();
@JsonAnySetter
public void setUserExtensions(String key, Object value) {
userExtensions.put(key, value);
}
public Map<String, Object> getUserExtensions() {
return userExtensions;
}
}
以下是我的 Main
class 生成 JSONSchema
:
class JsonSchemaGenerator{
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = TypeFactory.defaultInstance();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(typeFactory);
objectMapper.getDeserializationConfig().with(introspector);
objectMapper.getSerializationConfig().with(introspector);
//To force mapper to include JAXB annotated properties in Json schema
objectMapper.registerModule(new JaxbAnnotationModule());
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(Customer.class), visitor);
JsonSchema inputSchema = visitor.finalSchema();
String schemaString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(inputSchema);
System.out.println(schemaString);
}
}
我将 @JsonValue
与我的 Map<String,Object>
一起使用,这就是我只得到 type:any
的原因。我删除它然后尝试它对我有用。
@XmlRootElement(name = "Customer")
@XmlType(name = "Customer", propOrder = {"name", "age", "userExtensions"})
@XmlAccessorType(XmlAccessType.FIELD)
@NoArgsConstructor
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
public class Customer extends Person {
@XmlElement(name = "name",required = true)
private String name;
@XmlElement(name = "name",required = true)
private String age;
@JsonSerialize(using = CustomExtensionsSerializer.class)
@XmlJavaTypeAdapter(TestAdapter.class)
@XmlPath(".")
private Map<String, Object> userExtensions = new HashMap<>();
@JsonAnySetter
public void setUserExtensions(String key, Object value) {
userExtensions.put(key, value);
}
public Map<String, Object> getUserExtensions() {
return userExtensions;
}
}
class JsonSchemaGenerator{
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = TypeFactory.defaultInstance();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(typeFactory);
objectMapper.getDeserializationConfig().with(introspector);
objectMapper.getSerializationConfig().with(introspector);
//To force mapper to include JAXB annotated properties in Json schema
objectMapper.registerModule(new JaxbAnnotationModule());
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(Customer.class), visitor);
JsonSchema inputSchema = visitor.finalSchema();
String schemaString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(inputSchema);
System.out.println(schemaString);
}
}
我知道有很多问题与使用 Jackson
从 JAXB
注释 classes 生成 JSONSchema
有关,但我找不到任何示例使用 MOXY
注释生成的 JSONSchema class.
我只想知道如何为我的 MOXY
注释 class 生成 JSONSchema
?截至目前,当我为 JAXB/Moxy
注释 class 生成 JSONSchema
时,我只得到一个字段:
{
"type" : "any"
}
以下是我的 class,我想使用 Jackson
和 JAXB/Moxy
注释为其生成 JSONSchema
:(如您所见,我有 name
和 age
作为必填字段,但它们不会显示在生成的 JSONSchema
)
@XmlRootElement(name = "Customer")
@XmlType(name = "Customer", propOrder = {"name", "age", "userExtensions"})
@XmlAccessorType(XmlAccessType.FIELD)
@NoArgsConstructor
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
public class Customer extends Person {
@XmlElement(name = "name",required = true)
private String name;
@XmlElement(name = "name",required = true)
private String age;
@JsonSerialize(using = CustomExtensionsSerializer.class)
@XmlJavaTypeAdapter(TestAdapter.class)
@XmlPath(".")
@JsonValue
private Map<String, Object> userExtensions = new HashMap<>();
@JsonAnySetter
public void setUserExtensions(String key, Object value) {
userExtensions.put(key, value);
}
public Map<String, Object> getUserExtensions() {
return userExtensions;
}
}
以下是我的 Main
class 生成 JSONSchema
:
class JsonSchemaGenerator{
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = TypeFactory.defaultInstance();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(typeFactory);
objectMapper.getDeserializationConfig().with(introspector);
objectMapper.getSerializationConfig().with(introspector);
//To force mapper to include JAXB annotated properties in Json schema
objectMapper.registerModule(new JaxbAnnotationModule());
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(Customer.class), visitor);
JsonSchema inputSchema = visitor.finalSchema();
String schemaString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(inputSchema);
System.out.println(schemaString);
}
}
我将 @JsonValue
与我的 Map<String,Object>
一起使用,这就是我只得到 type:any
的原因。我删除它然后尝试它对我有用。
@XmlRootElement(name = "Customer")
@XmlType(name = "Customer", propOrder = {"name", "age", "userExtensions"})
@XmlAccessorType(XmlAccessType.FIELD)
@NoArgsConstructor
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
public class Customer extends Person {
@XmlElement(name = "name",required = true)
private String name;
@XmlElement(name = "name",required = true)
private String age;
@JsonSerialize(using = CustomExtensionsSerializer.class)
@XmlJavaTypeAdapter(TestAdapter.class)
@XmlPath(".")
private Map<String, Object> userExtensions = new HashMap<>();
@JsonAnySetter
public void setUserExtensions(String key, Object value) {
userExtensions.put(key, value);
}
public Map<String, Object> getUserExtensions() {
return userExtensions;
}
}
class JsonSchemaGenerator{
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = TypeFactory.defaultInstance();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(typeFactory);
objectMapper.getDeserializationConfig().with(introspector);
objectMapper.getSerializationConfig().with(introspector);
//To force mapper to include JAXB annotated properties in Json schema
objectMapper.registerModule(new JaxbAnnotationModule());
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
objectMapper.acceptJsonFormatVisitor(objectMapper.constructType(Customer.class), visitor);
JsonSchema inputSchema = visitor.finalSchema();
String schemaString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(inputSchema);
System.out.println(schemaString);
}
}