'How do I configure a Jackson ObjectMapper to show the root value?
In my Spring project i was added jackson 1 and now it is 2 then i have seen this difference. Previously response was cumming right with 'loginResponse' now no object name in Json .
Old object class for loginResponse as follows :
public class LoginResponse {
private String code;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
Following is my new object class for loginResponse with annotation and SerializationFeature.WRAP_ROOT_VALUE, false:
@JsonRootName(value = "loginResponse")
public class LoginResponse {
private String code;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
ObjectMapper aa= new ObjectMapper().configure(SerializationFeature.WRAP_ROOT_VALUE, false);
}
I want this output:
{
"loginResponse":
{
"code": 0
}
}
But it gives me the following response:
{
"page": 0
}
Please any one knows this. How to resolve? Please.
Solution 1:[1]
I believe SerializationFeature.WRAP_ROOT_VALUE should be set to true rather than false as in ur above snippet.
Solution 2:[2]
You set it to false instead of true.
Use this.
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ezhilxor |
| Solution 2 | Alozie Michael Chinedu |
