今天说下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 list = userService.findAll();
if(list.isEmpty()){
Json.toJson(new Result(false,7000,"没有数据",null),response);
return;
}
List listuser = new ArrayList();
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"
}]}