[英]How to use jersey to marshall custom arrayList object?
所以,我開始上這個課:
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。
有任何想法嗎?
好像@JsonRootResource似乎更多地保留給屬性,而不是類級別的聲明。 這應該會產生一個編譯錯誤,但不會。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.