NO IMAGE

今天說下json資料格式,規範的格式應該是包括資料返回狀態(一般就是false/true),狀態碼(比如200,7000,這個都是自己定義的),訊息(比如“獲取資料成功”/“獲取資料失敗”),資料物件,以前寫專案都是別人把方法寫好,自己去呼叫,今天自己也寫一個,其實很簡單,老規矩直接上程式碼;

首先定義返回實體Result,這裡面重點看下里面的建構函式,用了的方法的過載

package com.steven.utils.result;
/**
* 資料返回實體
* @author Administrator
*
*/
public class Result {
private Integer code;//狀態碼
private Boolean isSuccess;//狀態
private String massege;//訊息
private Object result;//資料物件
/**
* 無參構造器
*/
public Result(){
super();
}
/**
* 只返回狀態,狀態碼,訊息
* @param statu
* @param code
* @param massege
*/
public Result(Boolean success, Integer code, String massege){
super();
this.isSuccess=success;
this.code=code;
this.massege=massege;
}
/**
* 只返回狀態,狀態碼,資料物件
* @param statu
* @param code
* @param object
*/
public Result(Boolean success, Integer code, Object result){
super();
this.isSuccess=success;
this.code=code;
this.result=result;
}
/**
* 返回全部資訊即狀態,狀態碼,訊息,資料物件
* @param statu
* @param code
* @param massege
* @param result
*/
public Result(Boolean success, Integer code, String massege, Object result){
super();
this.isSuccess=success;
this.code=code;
this.massege=massege;
this.result=result;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public Boolean getIsSuccess() {
return isSuccess;
}
public void setIsSuccess(Boolean isSuccess) {
this.isSuccess = isSuccess;
}
public String getMassege() {
return massege;
}
public void setMassege(String massege) {
this.massege = massege;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}

Result實體建立了接下來還需要把轉換為json,我這裡用的fastJson,這方面的資料網上很多,下面看下Json這個類,

package com.steven.utils.json;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.steven.utils.result.Result;
/**
* 丟擲json資料格式
* @author QHY
*
*/
public class Json {
public static void toJson(Result result, HttpServletResponse response) throws Exception{
response.setContentType("text/json");
response.setHeader("Cache-Control", "no-cache");
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
String json = JSONObject.toJSONString(result, 
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.DisableCircularReferenceDetect);
writer.write(json);   	
writer.close();
}
}

最後就是測試程式碼了UserController,

/**
* 查詢所有使用者
* @throws Exception
*/
@RequestMapping(value="/user/findAll.htm",method=RequestMethod.GET)
public void findAll(HttpServletResponse response) throws Exception{
List<User> list = userService.findAll(); 
if(list.isEmpty()){
Json.toJson(new Result(false,7000,"沒有資料",null),response);
return;
}
List<User> listuser = new ArrayList<User>();
for (User entity : list) {
User user = new User();
user.setAdd_time(entity.getAdd_time());
user.setDelete_status(entity.getDelete_status());
user.setId(entity.getId());
user.setLast_login_time(entity.getLast_login_time());
user.setName(entity.getName());
user.setPwd(entity.getPwd());
listuser.add(entity);
}
Result result = new Result(true,200,"成功",listuser);
Json.toJson(result,response);
}

最後顯示結果

{
"code": 200,
"isSuccess": true,
"massege": "成功",
"result": [{
"add_time": 1521368553000,
"delete_status": false,
"id": 3,
"last_login_time": 1521368553000,
"name": "李四",
"pwd": "123456"
}]}