所以,首先我有这门课:
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.
}
}
和这个类:
public class UserList extends PaginatedList<User>{
public UserList(int startIndex, int endIndex...){
super(startIndex, endIndex...);
}
}
现在,当我的资源返回一个 UserList 时,它会按预期打印出用户列表:
[
{
"userName":"binky",
"age":115
}
]
但是,我希望输出是这样的:
{
"users":
[
{
"userName":"binky",
"age":115
}
],
"pagination":
{
"startIndex":5,
"endIndex":10
}
}
因此,我用@JSONRootName("") 对它们进行了注释。
@JsonRootName("pagination")
public class PaginatedList extends ArrayList<?>{}
@JsonRootName("activities")
public class UserList extends PaginatedList<User>{}
并创建了一个类来设置 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;
}
}
而且这个实现仍然不能解决我的问题。编组的 json 不返回未包装的根值。
我正在使用球衣 POJOMAPPING。
有任何想法吗?