A.java
@Entity
@Getter
@Setter
@Inheritance
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, inclue=JsonTypeInfo.As.PROPERTY, property="type")
@JsonSubTypes({
@JsonSubTypes.Type(value=AA.class,name="aa"),
@JsonSubTypes.Type(value=AB.class,name="ab"),
})
public abstract class A {
@Id
@GeneratedValue
private Long id;
//json ignore for getter
@ManyToOne
private A parent;
@OneToMany(mappedBy="parent")
private List<A> children;
}
AA.java
@Entity
@Getter
@Setter
@DiscriminatorValue("aa")
public class AA extends A{
private User user;
}
AB.java
@Entity
@Getter
@Setter
@DiscriminatorValue("ab")
public class AB extends A {
private String name;
}
Now, when I return an instance of class AB
as JSON, it looks like this:
{
"id": 1,
"type": "ab",
"children": [...],
"name": "ali"
}
Since I want to use a custom User
JSON, I want to use ModelMapper. All things are Ok except Absence of type
.
My DTOs:
@Getter
@Setter
public class ADto {
private Long id;
private List<ADto> children;
}
@Getter
@Setter
public class AADto extends ADto {
private UserDto user;
}
@Getter
@Setter
public class ABDto extends ADto {
private String name;
}
How can I use ModelMapper and get type
of class?
AB
is not used anywhere and it has only one filed but it's json has 4 fields. Could you reformat/shortly your questions so that it is clear what you need. – GlycerolAA
andAB
extends classA
. – Andros