3
实体类是这样的。
public class ProductWrapper<T extends Product>{
private T product;
private Integer quantity;
public ProductWrapper() {
}
public ProductWrapper(T product, Integer quantity) {
this.product = product;
this.quantity = quantity;
}
public T getProduct() {
return product;
}
public Integer getQuantity() {
return quantity;
}
public static <T extends Product> ProductWrapper<T> create(T product, Integer quantity){
return new ProductWrapper<>(product, quantity);
}
@Override
public String toString() {
return "ProductWrapper{" +
"product=" + product +
", quantity=" + quantity +
'}';
}
}
但是反序列化失败
ProductWrapper<ProductEntity> productEntityProductWrapper =
ProductWrapper.create(new ProductEntity("1L", "商品名字", "商品描述", BigDecimal.valueOf(11.11)), 100);
String jsonString = JSON.toJSONString(productEntityProductWrapper);
System.out.println(jsonString); {"product":{"description":"商品描述","id":"1L","name":"商品名字","price":11.11},"quantity":100}
ProductWrapper<ProductEntity> productEntityProductWrapper1 = JSON.parseObject(jsonString,
new TypeReference<ProductWrapper<ProductEntity>>(ProductEntity.class){});
System.out.println(productEntityProductWrapper1); ProductWrapper{product=null, quantity=null}
有什么解决办法吗?