1 @RequestMapping("/editReceiver") 2 public void editReceiver(HttpServletRequest request,HttpServletResponse response) { 3 int id = Integer.parseInt(request.getParameter("id")); 4 //System.out.println(id); 5 ReceiverInfo receiverInfo =receiverService.editRInfo(id); 6 //生成JSON: 7 Gson gson =new Gson(); 8 String json = gson.toJson(receiverInfo); 9 response.setCharacterEncoding("UTF-8"); 10 response.setContentType("application/json; charset=utf-8"); 11 PrintWriter writer = null; 12 try { 13 writer = response.getWriter(); 14 } catch (IOException e) { 15 e.printStackTrace(); 16 } 17 writer.append(json); 18 }
1 @RequestMapping("/JianSuanList") 2 public void jianSuanList(HttpServletRequest request,HttpServletResponse response) { 3 HttpSession session =request.getSession(); 4 @SuppressWarnings("unchecked")//用于取消一些编译器产生的警告对代码左侧行列的遮挡,有时候这会挡住我们断点调试时打的断点 5 List<LandCart> laCartsList = (List<LandCart>) session.getAttribute("list"); 6 Gson gson =new Gson(); 7 String json = gson.toJson(laCartsList); 8 response.setCharacterEncoding("UTF-8"); 9 response.setContentType("application/json; charset=utf-8"); 10 PrintWriter writer=null; 11 try { 12 writer=response.getWriter(); 13 } catch (IOException e) { 14 e.printStackTrace(); 15 } 16 writer.append(json); 17 }
需要import com.google.gson.Gson依赖
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency>