【发布时间】: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
【问题讨论】:
-
您可以使用
count和results创建一个新类ProductsWrapper,然后在getProducts方法中创建它的一个实例并返回它而不是列表。
标签: java json jpa jackson wildfly