我想使用傑克遜序列化一個對象包括一個通用的集合類型。 這是接口:傑克遜序列化通用集合
public interface PagingAdapter <Id extends Serializable, T extends Entity<Id>> extends Serializable {
public List<T> getItem();
public void setItem(List<T> items);
public Boolean hasNextPage();
public Integer getTotalPage();
public Integer getPageSize();
public void setPageSize(int pageSize);
public Long getTotalItem();
public void setTotalItem(Long totalItem);
public void setCurrentPage(Integer currentPage);
public Integer getCurrentPage();
public Class<T> getEntityType();
public void setEntityType(Class<T> entityType);
}
,這是實現:
public class PagingAdapterImpl<Id extends Serializable, T extends Entity<Id>> implements
PagingAdapter<Id,T> {
private static Integer DEFAULT_PAGE_SIZE = 20;
private Class<T> entityType;
private List<T> items = null;
private Integer pageSize = DEFAULT_PAGE_SIZE;
private Integer currentPage = 0;
private Long totalItem;
public PagingAdapterImpl(List<T> items, int currentPage, int pageSize, long totalItem) {
super();
this.items = items;
this.pageSize = pageSize;
this.currentPage = currentPage;
this.totalItem = totalItem;
}
public PagingAdapterImpl(){
}
@Override
public Class<T> getEntityType() {
return entityType;
}
@Override
public void setEntityType(Class<T> entityType) {
this.entityType = entityType;
}
@Override
public List<T> getItem() {
return items;
}
@Override
public void setItem(List<T> items) {
this.items = items;
}
@Override
public Boolean hasNextPage() {
return false;
}
@Override
public Integer getTotalPage() {
int rs = (int) (getTotalItem() % getPageSize() == 0 ? getTotalItem()
/getPageSize() : getTotalItem()/getPageSize() + 1);
return 0;
}
@Override
public Integer getPageSize() {
return this.pageSize;
}
@Override
public Long getTotalItem() {
return this.totalItem;
}
@Override
public void setTotalItem(Long totalItem) {
this.totalItem = totalItem;
}
@Override
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
@Override
public Integer getCurrentPage() {
return currentPage;
}
@Override
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
我使用RestEasy的傑克遜1.9。此對象的休息方法返回實例的輸出如下所示:
{
"status": 0,
"data": {
"entityType": null,
"pageSize": 1,
"currentPage": 1,
"totalItem": 1,
"item": [],
"totalPage": 0
}
}
「item」屬性無法序列化爲JSON數組。我該如何解決這個問題?
public class PagingAdapterSerializer extends JsonSerializer<PagingAdapter<Long, Entity<Long>>> {
@Override
public void serialize(PagingAdapter<Long, Entity<Long>> value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
jgen.writeObjectField("item",value.getItem());
jgen.writeObjectField("totalItem",value.getTotalPage());
jgen.writeObjectField("pageSize",value.getPageSize());
jgen.writeObjectField("totalItem",value.getTotalItem());
jgen.writeObjectField("currentPage",value.getCurrentPage());
jgen.writeEndObject();
}
這是我的自定義序列化程序。但它不起作用
你的對象沒有問題,你可以分享你如何調用傑克遜 – Bhavesh
我使用自定義傑克遜串行器,但很難指定泛型集合的實現類。它可以是任何實體類型。我想這就是爲什麼傑克遜不能序列化 – David