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.
All I want to know is how can I generate JSONSchema
for my MOXY
annotated class? As of now when I generate the JSONSchema
for my JAXB/Moxy
annotated class then I get only one field:
{
"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
)
@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
:
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);
}
}