Json返回通用对象,工具类
此工具类,用于返回json对象
=========================================================================================
/**
* 2017年10月20日下午3:29:05
*/
package com.jjmc.dcl.util;
import java.io.Serializable;
/**
*
* @author huangtao
* 2017年10月20日下午3:29:05
* dclTask
* @parameter
* TODO
*
*/
public class RespsonData implements Serializable {
private static final long serialVersionUID = -8516663678560534209L;
private String res;// 操作状态
private String msg;// 返回信息
private Object data; // 返回数据
public RespsonData(String res) {
this.res = res;
}
public RespsonData(String res, String msg) {
this.res = res;
this.msg = msg;
}
public void result(String res, String msg) {
this.res = res;
this.msg = msg;
}
public String getRes() {
return res;
}
public void setRes(String res) {
this.res = res;
}
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;
}
@Override
public String toString() {
return "RespsonData{" +
"res='" + res + '\'' +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
===============================================================================
引用方法
在需要返回结果的接口类中
private RespsonData rd = new RespsonData("success");// 通用返回JSON对象
接口使用@ResponseBody注解,会自动把结果转换成json格式
此为测试接口,成功返回的是success,失败返回的是error的json格式
private RespsonData rd = new RespsonData("success");
@RequestMapping(method={RequestMethod.POST}, value="/XXXXXX")
@ResponseBody
protected Object getXXXXXXX(HttpServletRequest request, HttpServletResponse response) throws Exception{
try {
} catch (Exception e) {
rd.setRes("error");
throw e;
}
return rd;
}