本节学会使用如何将数据改为JSON,传送到前台。
为什么用JSON?
当Android,IOS, 浏览器向服务器发送请求数据的时候,若后台返回的是JSON数据,这三者均易解析数据,体现了平台无关性。
使用步骤:
1 加入所需要的jar包依赖:
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.8.1</version> </dependency>
2 后台代码改为接口的形式,@ResponseBody即可以将数据变为JSON串的形式:
/** * 分页查询 * @param pageNo 页码 * @return */ //返回JSON @RequestMapping(value = {"/emp/list"}, method = {RequestMethod.GET}) @ResponseBody public PageInfo getEmpList(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo){ PageHelper.startPage(pageNo, 5); List<Employee> employeeList = employeeService.getAll(); PageInfo pageInfo = new PageInfo(employeeList, 5); return pageInfo; }
返回结果如下:
{"pageNum":5,"pageSize":5,"size":5,"startRow":21,"endRow":25,"total":1001,"pages":201,
"list":[{"empId":21,"empName":"c36ab","gender":"M","eamil":"c36ab@qq.com","dId":1,"department":{"deptId":1,"deptName":"开发部"}},
{"empId":22,"empName":"d36f0","gender":"M","eamil":"d36f0@qq.com","dId":1,"department":{"deptId":1,"deptName":"开发部"}},
{"empId":23,"empName":"8d86c","gender":"M","eamil":"8d86c@qq.com","dId":1,"department":{"deptId":1,"deptName":"开发部"}},
{"empId":24,"empName":"21049","gender":"M","eamil":"21049@qq.com","dId":1,"department":{"deptId":1,"deptName":"开发部"}},
{"empId":25,"empName":"df993","gender":"M","eamil":"df993@qq.com","dId":1,"department":{"deptId":1,"deptName":"开发部"}}],
"prePage":4,"nextPage":6,"isFirstPage":false,"isLastPage":false,"hasPreviousPage":true,"hasNextPage":true,"navigatePages":5,"navigatepageNums":[3,4,5,6,7],"navigateFirstPage":3,"navigateLastPage":7,"firstPage":3,"lastPage":7}
3 优化,带有返回信息的代码:
信息类:Msg.java
package com.wgs.domain; import java.util.HashMap; import java.util.Map; /** * Created by GenshenWang.nomico on 2017/6/9. */ public class Msg { private int code; private String msg; private Map<String, Object> extendInfo = new HashMap<>(); public static Msg success(){ Msg result = new Msg(); result.setCode(100); result.setMsg("操作成功"); return result; } public static Msg fail(){ Msg result = new Msg(); result.setCode(200); result.setMsg("操作失败"); return result; } public Msg add(String key, Object value){ this.getExtendInfo().put(key, value); return this; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Map<String, Object> getExtendInfo() { return extendInfo; } public void setExtendInfo(Map<String, Object> extendInfo) { this.extendInfo = extendInfo; } }
接口:
//返回JSON @RequestMapping(value = {"/emp/list"}, method = {RequestMethod.GET}) @ResponseBody public Msg getEmpListWithJson(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo){ PageHelper.startPage(pageNo, 5); List<Employee> employeeList = employeeService.getAll(); PageInfo pageInfo = new PageInfo(employeeList, 5); return Msg.success().add("pageInfo", pageInfo); }
返回结果:
{
"code":100,
"msg":"操作成功",
"extendInfo":{
"pageInfo":{"pageNum":5,"pageSize":5,"size":5,"startRow":21,"endRow":25,"total":1001,"pages":201,
"list":[{"empId":21,"empName":"c36ab","gender":"M","eamil":"c36ab@qq.com","dId":1,"department":{"deptId":1,"deptName":"开发部"}},{"empId":22,"empName":"d36f0","gender":"M","eamil":"d36f0@qq.com","dId":1,"department":{"deptId":1,"deptName":"开发部"}},{"empId":23,"empName":"8d86c","gender":"M","eamil":"8d86c@qq.com","dId":1,"department":{"deptId":1,"deptName":"开发部"}},{"empId":24,"empName":"21049","gender":"M","eamil":"21049@qq.com","dId":1,"department":{"deptId":1,"deptName":"开发部"}},{"empId":25,"empName":"df993","gender":"M","eamil":"df993@qq.com","dId":1,"department":{"deptId":1,"deptName":"开发部"}}],"prePage":4,"nextPage":6,"isFirstPage":false,"isLastPage":false,"hasPreviousPage":true,"hasNextPage":true,"navigatePages":5,"navigatepageNums":[3,4,5,6,7],"navigateFirstPage":3,"navigateLastPage":7,"firstPage":3,"lastPage":7}}}