For a Java code with the following annotation:
@JsonProperty(value="Contact")
@NotNull(message = "ContactUser or CompanyName is required.")
@Valid
@XmlElements(value = {
@XmlElement(name = "ContactUser", type = ContactUser.class, required = true),
@XmlElement(name = "CompanyName", type = String.class, required = true) })
private Object contactInfo;
Result set when I use the object for GET is :
"Contact": {
"ContactUser": {
"Title": "Miss",
"LastName": "Hello"
}
}
or
"Contact": "Hello Company"
Is there a way so it returns:
"ContactUser": {
"Title": "Miss",
"LastName": "Hello"
}
or
"CompanyName": "Hello Company"
instead? In xml, with the code, you can do:
<CompanyName>Hello Company</CompanyName>
or
<ContactUser>
<Title>Miss</Title>
<LastName>Hello</LastName>
</ContactUser>
I have tried using JsonTypeInfo but it doesnt seem to handle String.class:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include =JsonTypeInfo.As.WRAPPER_OBJECT, visible=true)
@JsonSubTypes({
@JsonSubTypes.Type(name = "ContactUserJ", value = ContactUser.class),
@JsonSubTypes.Type(name = "CompanyNameJ" , value = String.class)
})