Issue
I am creating a JSON mapper that will create JSON schema for my JPA database classes. I am using mbknor-jackson-jsonSchema
that works great, but I need to serialize my subclasses as id
only.
So far my structure is:
public class ModelPojo{
private List<Table> table;
private List<Table2> table2;
}
Both table classes look similar to this:
@Table(name = "TABLE")
public class Table extends BaseEntity {
@Column(name = "SMTH")
private String smth;
@JoinColumn(name = "TABLE2")
private Table2 table2; //now this is where is the problem
}
BaseEntity
contains integer id
field.
The question is, is there a way to write custom std serializer that serializes table entity to have property id_table2: "integer"
, but not the whole object?
I tried to override serialize method for StdSerializer<BaseEntity>
which does not work, it is not called when creating the schema
Edit: I now get
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Model Pojo",
"type": "object",
"additionalProperties": false,
"properties": {
"Table": {
"type": "array",
"items": {
"$ref": "#/definitions/Table"
}
}
"Table2": {
"type": "array",
"items": {
"$ref": "#/definitions/Table2"
}
}
},
"definitions": {
"Table": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "integer"
},
"table2": {
"$ref": "#/definitions/Table2"
},
"smth": {
"type": "string"
}
}
},
"Table2": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "integer"
},
"foo": {
"type": "string"
}
}
}
}
}
and i want to change
"table2": {
"$ref": "#/definitions/Table2"
},
to
"table2_id": {
"type": "integer"
},
The structure of joined tables is much more complex, so I am trying to not manually add @JsonIgnore
and changing it manually, but write some type of serializer that retypes the child instances of BaseEntity
to id
, hope it's understandable.
Solution
I made some changes, I changed JSON serialization framework to victools/jsonschema-generator, which allowed me to heavily modify its serialization
I then serialized with customDefinitionProvider. It helped me to change JsonNode names and most importantly I could chose what I want to serialize through code not via Json tags.
Thanks @Carsten for great work :)
Answered By - konselik
Answer Checked By - David Marino (JavaFixing Volunteer)