【问题标题】:Jackson Serialize List Entities (add field to root list)Jackson 序列化列表实体(将字段添加到根列表)
【发布时间】:2017-07-16 15:47:34
【问题描述】:

这是我的实体

@Entity
public class Product extends AbstractBaseEntity {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Type(type = "objectid")
   private String id;
   private String title;

我的资源

@Path(value = ApiConstant.Urls.PRODUCTS)
public class ProductResource {

   @Inject
   private ProductService productService;

   @GET
   @Path(value = ApiConstant.Urls.PRODUCTS)
   @Produces(value = MediaType.APPLICATION_JSON)
   public List getProducts(){
      return productService.findAll();
   }

我的 json 响应

[ {
  "id" : "596b6a02f70a0878590bcf08",
  "title" : "test1",
  "description" : "description test 1"
}, {
  "id" : "596b6b00f70a087b72d377eb",
  "title" : "test1",
  "description" : "description test 1"
}, {
  "id" : "596b6b75f70a087d40f580d5",
  "title" : "test1",
  "description" : "description test 1"
} ]

我想创建一个计数字段来计算列表中的项目 像这样并将列表添加到结果字段中

{
    "count": 3,
    "results":  [ 
     {
      "id" : "596b6a02f70a0878590bcf08",
      "title" : "test1",
      "description" : "description test 1"
    }, {
      "id" : "596b6b00f70a087b72d377eb",
      "title" : "test1",
      "description" : "description test 1"
    }, {
      "id" : "596b6b75f70a087d40f580d5",
      "title" : "test1",
      "description" : "description test 1"
    } ], 
}

我要序列化 ​​jpa persistence 返回的 Product List

【问题讨论】:

  • 您可以使用countresults 创建一个新类ProductsWrapper,然后在getProducts 方法中创建它的一个实例并返回它而不是列表。

标签: java json jpa jackson wildfly


【解决方案1】:

您可以使用以下类来包含计数以及Product 实体列表:

public class ResultList {
    private int count;
    @JsonProperty("results") private List<Product> products;

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = Objects.requireNonNull(products, "products");
        this.count = products.size();
    }

    public int getCount() {
        return count;
    }
}

【讨论】:

    【解决方案2】:

    具有泛型类型的类

    public class ResultList<T> {
    
        private int count;
        @JsonProperty("results")
        private List<T> items;
    
        public List<T> getItems() {
            return items;
        }
    
        public void setItems(List<T> items) {
            this.items = Objects.requireNonNull(items, "items");
            this.count = items.size();
        }
    
        public int getCount() {
            return count;
        }
    }
    

    产品资源

    @GET
    @Path(value = ApiConstant.Urls.PRODUCTS)
    @Produces(value = MediaType.APPLICATION_JSON)
    public ResultList getProducts(){
        List products = productService.findAll();
        ResultList result = new ResultList<Product>();
        result.setItems(products);
        return result;
    
    }
    

    谢谢@ck1

    【讨论】:

      猜你喜欢
      • 2016-01-13
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 2022-08-14
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多