如何通过在spring MVC项目中使用条件投影来获取json数据。 我的标准查询如下。
public List<Item> getProduct() {
Criteria crit = session.createCriteria(Item.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("item_code"));
projList.add(Projections.property("item_name"));
crit.setProjection(projList);
List<Item> results = crit.list();
}
我的控制器类跟随
@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public @ResponseBody List<Item> ProductList() {
List<Item> productList = poService.getProduct();
Gson gson = new GsonBuilder().serializeNulls().create();
System.out.println("json = " + gson.toJson(productList));
return productList;
}
输出为
[[3,"CELLO PINPOINT"],[5,"JOHNSON BUDS 30"],[7,"SMALL WONDER FEEDING BOTTLE 125 ML"],[45,"YERA GLASS TUMBLER T70 6PC"],[1623,"PEPSI 500 ML"],[1816,"FOOD CHOPPER"],[1819,"SCREW SET"],[5653,"AMUL KOOL200ML"]]
面对问题,因为没有以json格式输出。