在servlet层添加代码:
String name = req.getParameter("username");
ArrayList<Order> List = new OrderDAO().findOrder(name);
JsonArray jsonArray=new JsonArray();
String str="";
resp.setContentType("application/json;charset=utf-8");
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();
for(int i=0;i<List.size();i++){
JsonObject jsonOb=new JsonObject();
jsonOb.addProperty("food", List.get(i).getFood());
jsonOb.addProperty("seller", List.get(i).getSeller());
jsonOb.addProperty("price", List.get(i).getPrice());
jsonOb.addProperty("stage", List.get(i).getStage());
jsonOb.addProperty("time", List.get(i).getTime().toLocaleString());
jsonArray.add(jsonOb);
}
out.printf(jsonArray.toString());
实现后浏览器打开后端url得到结果:
[