I have a class like below. When I serialize using Jackson library, I want the 'StudentClass' object name in the Json as 'class' and not 'studentClass'. I tried few things but did not work out. Please see below
public class Classification {
private Department department;
private StudentClass studentClass;
// getters and setters
}
public class Department {
private Integer id;
private String title;
// getters and setters
}
public class StudentClass {
private Integer id;
private String title;
// getters and setters
}
Expected output:
{
"classification": {
"department": {
"id": 123,
"title": "Healthcare"
},
"class": {
"id": 456,
"title": "blah"
}
}
}
Actual output:
{
"classification": {
"department": {
"id": 123,
"title": "Healthcare"
},
"studentClass": {
"id": 456,
"title": "blah"
}
}
}
I tried adding below config on StudentClass, however, it yielded me a different result as below
@JsonTypeName("class")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
public class StudentClass {
private Integer id;
private String title;
// getters and setters
}
Output:
{
"classification": {
"department": {
"id": 123,
"title": "Healthcare"
},
"studentClass": {
"class": {
"id": 456,
"title": "blah"
}
}
}
}
Well I had to just put @JsonProperty("class") like below and it worked. My bad, I was running behind adding a different config.