简体   繁体   English

如何为 JAXB/Moxy 注释的 POJO class 生成 JSONSchema

[英]How to generate the JSONSchema for the JAXB/Moxy annotated POJO class

I know there are a lot of questions related to generating the JSONSchema from JAXB annotated classes using the Jackson but I could not find any example where JSONSchema generated using the MOXY annotated class.我知道有很多与使用JSONSchemaJAXB注释类生成 JSONSchema 相关的问题,但我找不到使用Jackson注释 ZA2F2ED4F8EBC2CBB4C21A29DC40AB 生成MOXY的任何示例。

All I want to know is how can I generate JSONSchema for my MOXY annotated class?我只想知道如何为我的MOXY注释 class 生成JSONSchema As of now when I generate the JSONSchema for my JAXB/Moxy annotated class then I get only one field:到目前为止,当我为我的JAXB/Moxy注释的 class 生成JSONSchema时,我只得到一个字段:

{
  "type" : "any"
}

Following is my class for which I would like to generate JSONSchema using the Jackson and JAXB/Moxy annotations: (As you can see I have name and age as mandatory field but they don't show up in the generated JSONSchema )以下是我的 class ,我想使用JacksonJAXB/Moxy注释为其生成JSONSchema :(如您所见,我将nameage作为必填字段,但它们没有显示在生成的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;
  }
}

Following is my Main class which generates the JSONSchema :以下是生成 JSONSchema 的Main 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);
  }
}

I am using the @JsonValue with my Map<String,Object> that's the reason I was getting just type:any .我将@JsonValue与我的Map<String,Object>一起使用,这就是我得到type:any的原因。 I removed it and then tried and it worked for me.我删除了它,然后尝试了它,它对我有用。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM