0 课程地址
https://www.imooc.com/video/16786
1 返回json对象常见差异
返回json对象时,类上也要加@RequestMapping注解。
2 返回json对象demo
2.1 返回json对象
bean类:
package com.example.demo.bean; import java.util.Date; /** * User * * @author 魏豆豆 * @date 2020/11/15 */ public class User { private String userID; private String userName; private int userAge; private String passWord; private Date birthDay; private String desc; public String getUserID() { return userID; } public void setUserID(String userID) { this.userID = userID; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getUserAge() { return userAge; } public void setUserAge(int userAge) { this.userAge = userAge; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } }
Controller类:
package com.example.demo.son.demob; import com.example.demo.bean.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.Date; /** * UserControllerA * 目的:返回json对象 * @author 魏豆豆 * @date 2020/11/15 */ @Controller @RequestMapping("/user") public class UserControllerA { @RequestMapping("/getUser") @ResponseBody public Object getUser(){ User u = new User(); u.setUserID("20201115001"); u.setUserName("大黄"); u.setPassWord("dahuang"); u.setBirthDay(new Date()); u.setUserAge(18); return u; } }
测试:
2.2 返回封装的json对象
bean类:
package com.example.demo.bean;
import java.util.Date;
/**
* User
*
* @author 魏豆豆
* @date 2020/11/15
*/
public class User {
private String userID;
private String userName;
private int userAge;
private String passWord;
private Date birthDay;
private String desc;
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
Controller类:
package com.example.demo.son.demob; import com.example.demo.bean.User; import com.example.demo.tools.JSONResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.Date; /** * UserControllerA * 目的:返回json对象 * @author 魏豆豆 * @date 2020/11/15 */ @RestController @RequestMapping("/user") public class UserControllerB { @RequestMapping("/getUserJson") public JSONResult getUser(){ User u = new User(); u.setUserID("20201115001"); u.setUserName("大黄"); u.setPassWord("dahuang"); u.setBirthDay(new Date()); u.setUserAge(18); return JSONResult.ok(u); } }
封装类:
package com.example.demo.tools; import java.util.List; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; /** * * @Title: LeeJSONResult.java * @Package com.lee.utils * @Description: 自定义响应数据结构 * 这个类是提供给门户,ios,安卓,微信商城用的 * 门户接受此类数据后需要使用本类的方法转换成对于的数据类型格式(类,或者list) * 其他自行处理 * 200:表示成功 * 500:表示错误,错误信息在msg字段中 * 501:bean验证错误,不管多少个错误都以map形式返回 * 502:拦截器拦截到用户token出错 * 555:异常抛出信息 * Copyright: Copyright (c) 2016 * Company:Nathan.Lee.Salvatore * * @author leechenxiang * @date 2016年4月22日 下午8:33:36 * @version V1.0 */ public class JSONResult { // 定义jackson对象 private static final ObjectMapper MAPPER = new ObjectMapper(); // 响应业务状态 private Integer status; // 响应消息 private String msg; // 响应中的数据 private Object data; private String ok; // 不使用 public static JSONResult build(Integer status, String msg, Object data) { return new JSONResult(status, msg, data); } public static JSONResult ok(Object data) { return new JSONResult(data); } public static JSONResult ok() { return new JSONResult(null); } public static JSONResult errorMsg(String msg) { return new JSONResult(500, msg, null); } public static JSONResult errorMap(Object data) { return new JSONResult(501, "error", data); } public static JSONResult errorTokenMsg(String msg) { return new JSONResult(502, msg, null); } public static JSONResult errorException(String msg) { return new JSONResult(555, msg, null); } public JSONResult() { } // public static LeeJSONResult build(Integer status, String msg) { // return new LeeJSONResult(status, msg, null); // } public JSONResult(Integer status, String msg, Object data) { this.status = status; this.msg = msg; this.data = data; } public JSONResult(Object data) { this.status = 200; this.msg = "OK"; this.data = data; } public Boolean isOK() { return this.status == 200; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } /** * * @Description: 将json结果集转化为LeeJSONResult对象 * 需要转换的对象是一个类 * @param jsonData * @param clazz * @return * * @author leechenxiang * @date 2016年4月22日 下午8:34:58 */ public static JSONResult formatToPojo(String jsonData, Class<?> clazz) { try { if (clazz == null) { return MAPPER.readValue(jsonData, JSONResult.class); } JsonNode jsonNode = MAPPER.readTree(jsonData); JsonNode data = jsonNode.get("data"); Object obj = null; if (clazz != null) { if (data.isObject()) { obj = MAPPER.readValue(data.traverse(), clazz); } else if (data.isTextual()) { obj = MAPPER.readValue(data.asText(), clazz); } } return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj); } catch (Exception e) { return null; } } /** * * @Description: 没有object对象的转化 * @param json * @return * * @author leechenxiang * @date 2016年4月22日 下午8:35:21 */ public static JSONResult format(String json) { try { return MAPPER.readValue(json, JSONResult.class); } catch (Exception e) { e.printStackTrace(); } return null; } /** * * @Description: Object是集合转化 * 需要转换的对象是一个list * @param jsonData * @param clazz * @return * * @author leechenxiang * @date 2016年4月22日 下午8:35:31 */ public static JSONResult formatToList(String jsonData, Class<?> clazz) { try { JsonNode jsonNode = MAPPER.readTree(jsonData); JsonNode data = jsonNode.get("data"); Object obj = null; if (data.isArray() && data.size() > 0) { obj = MAPPER.readValue(data.traverse(), MAPPER.getTypeFactory().constructCollectionType(List.class, clazz)); } return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj); } catch (Exception e) { return null; } } public String getOk() { return ok; } public void setOk(String ok) { this.ok = ok; } }
测试:
3 常见状态码
* @Description: 自定义响应数据结构 * 这个类是提供给门户,ios,安卓,微信商城用的 * 门户接受此类数据后需要使用本类的方法转换成对于的数据类型格式(类,或者list) * 其他自行处理 * 200:表示成功 * 500:表示错误,错误信息在msg字段中 * 501:bean验证错误,不管多少个错误都以map形式返回 * 502:拦截器拦截到用户token出错 * 555:异常抛出信息
4 使用的注解
@RestController = @Controller + @ResponsBody(见demo1 和demo2 对比标红部分)
@ResponsBody表示返回的对象是一个json字符串