Spring Boot介面返回json
阿新 • • 發佈:2018-11-11
一 實體類
1 User
package com.imooc.pojo; import java.util.Date; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; public class User { private String name; //忽略該欄位 @JsonIgnore private String password; private Integer age; //格式化時間 @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss a", locale="zh", timezone="GMT+8") private Date birthday; //如果該欄位為空就不顯示 @JsonInclude(Include.NON_NULL) private String desc; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } 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; } }
2 IMoocJSONResult
package com.imooc.pojo; import java.util.List; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; /** * * @Title: IMoocJSONResult.java * @Description: 自定義響應資料結構,將JSON資料封裝到一個物件中 * 這個類是提供給門戶,ios,安卓,微信商城用的 * 門戶接受此類資料後需要使用本類的方法轉換成對於的資料型別格式(類,或者list) * 其他自行處理 * 200:表示成功 * 500:表示錯誤,錯誤資訊在msg欄位中 * 501:bean驗證錯誤,不管多少個錯誤都以map形式返回 * 502:攔截器攔截到使用者token出錯 * 555:異常丟擲資訊 */ public class IMoocJSONResult { // 定義jackson物件 private static final ObjectMapper MAPPER = new ObjectMapper(); // 響應業務狀態 private Integer status; // 響應訊息 private String msg; // 響應中的資料 private Object data; private String ok; public static IMoocJSONResult build(Integer status, String msg, Object data) { return new IMoocJSONResult(status, msg, data); //3個引數 } public static IMoocJSONResult ok(Object data) { return new IMoocJSONResult(data); //1個引數 } public static IMoocJSONResult ok() { return new IMoocJSONResult(null); //data為空進行構造 } public static IMoocJSONResult errorMsg(String msg) { return new IMoocJSONResult(500, msg, null); //500:表示錯誤,錯誤資訊在msg欄位中 } public static IMoocJSONResult errorMap(Object data) { return new IMoocJSONResult(501, "error", data); //501:bean驗證錯誤 } public static IMoocJSONResult errorTokenMsg(String msg) { return new IMoocJSONResult(502, msg, null); //502:攔截器攔截到使用者token出錯 } public static IMoocJSONResult errorException(String msg) { return new IMoocJSONResult(555, msg, null); //555:異常丟擲資訊 } public IMoocJSONResult() { } //3參建構函式 public IMoocJSONResult(Integer status, String msg, Object data) { this.status = status; this.msg = msg; this.data = data; } //1參建構函式 public IMoocJSONResult(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 */ public static IMoocJSONResult formatToPojo(String jsonData, Class<?> clazz) { try { if (clazz == null) { return MAPPER.readValue(jsonData, IMoocJSONResult.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 */ public static IMoocJSONResult format(String json) { try { return MAPPER.readValue(json, IMoocJSONResult.class); } catch (Exception e) { e.printStackTrace(); } return null; } /** * * @Description: Object是集合轉化 * 需要轉換的物件是一個list * @param jsonData * @param clazz * @return * */ public static IMoocJSONResult 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; } }
二 控制器
package com.imooc.controller; import java.util.Date; 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 com.imooc.pojo.IMoocJSONResult; import com.imooc.pojo.User; //@Controller @RestController // @RestController = @Controller + @ResponseBody @RequestMapping("/user") public class UserContoller { @RequestMapping("/getUser") // @ResponseBody public User getUser() { User u = new User(); u.setName("cakin"); u.setAge(35); u.setBirthday(new Date()); u.setPassword("fsghkaf"); u.setDesc("hello cakin"); //該欄位顯示 return u; } @RequestMapping("/getUserJson") // @ResponseBody public IMoocJSONResult getUserJson() { User u = new User(); u.setName("cakin"); u.setAge(35); u.setBirthday(new Date()); u.setPassword("fsghkaf"); // u.setDesc("hello cakin"); //該欄位不顯示 return IMoocJSONResult.ok(u); } }
三 測試
1 輸入: http://localhost:8080/user/getUser
輸出
{
"name": "cakin",
"age": 35,
"birthday": "2018-11-10 08:13:59 下午",
"desc": "hello cakin"
}
2 輸入: http://localhost:8080/user/getUserJson
輸出:
{
"status": 200,
"msg": "OK",
"data": {
"name": "cakin",
"age": 35,
"birthday": "2018-11-10 08:15:06 下午"
},
"ok": null
}