So, to start I have this class:
public class PaginatedList<?> extends ArrayList<?>{
private int startIndex;
private int endIndex;
public PaginatedList(int startIndex, int endIndex...){
this.startIndex = startIndex;
this.endIndex = endIndex;
//Do pagination stuffs.
}
}
and this class:
public class UserList extends PaginatedList<User>{
public UserList(int startIndex, int endIndex...){
super(startIndex, endIndex...);
}
}
Now, when I have my resource return a UserList, it prints out the list of users as expected:
[
{
"userName":"binky",
"age":115
}
]
But, I want the output to be like this:
{
"users":
[
{
"userName":"binky",
"age":115
}
],
"pagination":
{
"startIndex":5,
"endIndex":10
}
}
So, I annotate both of them with @JSONRootName(“”).
@JsonRootName("pagination")
public class PaginatedList extends ArrayList<?>{}
@JsonRootName("activities")
public class UserList extends PaginatedList<User>{}
and created a class to set up the ObjectMapper:
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class Resolver implements ContextResolver<ObjectMapper>{
private ObjectMapper objectMapper;
public ObjectMapperProvider(){
objectMapper = new ObjectMapper();
}
@Override
public ObjectMapper getContext(Class<?> type) {
if(type.getAnnotation(JsonRootName.class) != null){
objectMapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
}else{
objectMapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, false);
}
return objectMapper;
}
}
And still this implementation does not resolve my issue. The marshalled json does not return the unwrapped root values.
I am using jersey POJOMAPPING.
Any ideas?
It seems as though @JsonRootResource is more reserved for properties, not class level declarations. This should create a compilation error, but does not.