BaseController.class
/**
* Controller基类
*/
public class BaseController {
private Logger logger = LoggerFactory.getLogger(getClass());
/** 返回状态键名 **/
private static final String KEY_CODE = "code";
/** 返回数据键名 **/
private static final String KEY_DATA = "data";
/** 返回信息键名 **/
private static final String KEY_MSG = "msg";
/** 代表成功的值 **/
private static final String VALUE_SUCCESS = "200";
/** 代表错误的值 **/
private static final String VALUE_ERROR = "300";
/** 系统异常 **/
private static final String SYSTEM_ERROR = "999";
/**
* 封装并以json返回成功执行的信息
* @param response
* @param data
* @param msg
*/
protected void respSuccessMsg(HttpServletResponse response, Object data, String msg) {
Map<String, Object> map = new HashMap<String, Object>();
try {
map.put(KEY_DATA, data);
map.put(KEY_CODE, VALUE_SUCCESS);
map.put(KEY_MSG, msg);
//response.setContentType("application/json");
response.setContentType("text/html");
//response输出字符串
ContextUtils.respString(response, JSON.toJSONString(map,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullNumberAsZero));
} catch (Exception e) {
logger.error("respSuccessMsg 异常",e);
}
}
protected void respSuccessMsg8(HttpServletResponse response, Object data,Object backId,Boolean isNeedBack, String msg,String code) {
Map<String, Object> map = new HashMap<String, Object>();
try {
map.put(KEY_DATA, data);
map.put(KEY_CODE, code);
map.put(KEY_MSG, msg);
map.put("backId",backId);
map.put("isNeedBack",isNeedBack);
//response.setContentType("application/json");
response.setContentType("text/html");
//response输出字符串
ContextUtils.respString(response, JSON.toJSONString(map,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullNumberAsZero));
} catch (Exception e) {
logger.error("respSuccessMsg 异常",e);
}
}
protected void respSuccessMsg1(HttpServletResponse response, Object data, String msg) {
Map<String, Object> map = new HashMap<String, Object>();
try {
map.put(KEY_DATA, data);
map.put(KEY_CODE, VALUE_SUCCESS);
map.put(KEY_MSG, msg);
//response.setContentType("application/json");
//response.setContentType("text/html");
response.setContentType("application/octet-stream");
//response输出字符串
ContextUtils.respString(response, JSON.toJSONString(map));
} catch (Exception e) {
logger.error("respSuccessMsg 异常",e);
}
}
/**
* 封装并以json返回错误执行的信息
* @param response
* @param msg
*/
protected void respErrorMsg(HttpServletResponse response, String msg) {
Map<String, Object> map = new HashMap<String, Object>();
try {
map.put(KEY_CODE, VALUE_ERROR);
map.put(KEY_MSG, msg);
// response.setContentType("application/json");
// 解決 IE9 JSON 出现下载的问题
response.setContentType("text/html");
ContextUtils.respString(response, JSON.toJSONString(map));
} catch (Exception e) {
logger.error("respErrorMsg 异常",e);
}
}
/**
* 封装并以json返回错误执行的信息
* @param response
* @param msg
*/
protected void respErrorMsg(HttpServletResponse response,Object data, String msg) {
Map<String, Object> map = new HashMap<String, Object>();
try {
map.put(KEY_DATA,data);
map.put(KEY_CODE, VALUE_ERROR);
map.put(KEY_MSG, msg);
// response.setContentType("application/json");
// 解決 IE9 JSON 出现下载的问题
response.setContentType("text/html");
ContextUtils.respString(response, JSON.toJSONString(map));
} catch (Exception e) {
logger.error("respErrorMsg 异常",e);
}
}
/** 封装并以json返回错误执行的信息
* @param response
* @param msg
*/
public void respErrorMsg(HttpServletResponse response, String code, String msg) {
Map<String, Object> map = new HashMap<String, Object>();
try {
map.put(KEY_CODE, code);
map.put(KEY_MSG, msg);
response.setContentType("text/html");
//response.setContentType("application/json");
ContextUtils.respString(response, JSON.toJSONString(map));
} catch (Exception e) {
logger.error("respErrorMsg 异常",e);
}
}
/** 封装并以json返回错误执行的信息
* @param response
* @param msg
*/
public void respErrorMsg(HttpServletResponse response, String code, String msg,Object data) {
Map<String, Object> map = new HashMap<String, Object>();
try {
map.put(KEY_DATA,data);
map.put(KEY_CODE, code);
map.put(KEY_MSG, msg);
response.setContentType("text/html");
//response.setContentType("application/json");
ContextUtils.respString(response, JSON.toJSONString(map));
} catch (Exception e) {
logger.error("respErrorMsg 异常",e);
}
}
/**
* 获取当前登录的user
*/
public User getLoginUser() {
Subject subject = SecurityUtils.getSubject();
if (subject != null) {
Object object = subject.getPrincipal();
if (object != null) {
return (User) object;
}
}
return null;
}
/**
* 获取当前登录的userId
*/
public Integer getLoginUserId() {
User loginUser = getLoginUser();
return loginUser == null ? null : loginUser.getUserId();
}
/**
* 获取当前登录的username
*/
public String getLoginUserName() {
User loginUser = getLoginUser();
return loginUser == null ? null : loginUser.getUsername();
}
}
ContextUtils.class工具类
public class ContextUtils {
/**
* response输出字符串
* @param response
* @param str
* @throws Exception
*/
public static void respString(HttpServletResponse response, String str) throws Exception {
try {
// 设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setCharacterEncoding("UTF-8");
PrintWriter out = null;
out = response.getWriter();
out.print(str);
out.flush();
out.close();
} catch (Exception e) {
throw e;
}
}
}
JsonResult.class 封装工具类
/**
* 返回结果对象
*/
public class JsonResult extends HashMap<String, Object> {
private static final long serialVersionUID = 1L;
private JsonResult() {
}
/**
* 返回成功
*/
public static JsonResult ok() {
return ok("操作成功");
}
/**
* 返回成功
*/
public static JsonResult ok(String message) {
return ok(200, message);
}
/**
* 返回成功
*/
public static JsonResult ok(int code, String message) {
JsonResult jsonResult = new JsonResult();
jsonResult.put("code", code);
jsonResult.put("msg", message);
return jsonResult;
}
/**
* 返回失败
*/
public static JsonResult error() {
return error("操作失败");
}
/**
* 返回失败
*/
public static JsonResult error(String messag) {
return error(500, messag);
}
/**
* 返回失败
*/
public static JsonResult error(int code, String message) {
return ok(code, message);
}
/**
* 设置code
*/
public JsonResult setCode(int code) {
super.put("code", code);
return this;
}
/**
* 设置message
*/
public JsonResult setMessage(String message) {
super.put("msg", message);
return this;
}
/**
* 放入object
*/
@Override
public JsonResult put(String key, Object object) {
super.put(key, object);
return this;
}
}
PageResult.class 分页工具类(mybatis-plus)
/**
* 分页结果对象,这里以layui框架的table为标准
*/
public class PageResult<T> {
private int code; // 状态码, 0表示成功
private String msg; // 提示信息
private long count; // 总数量, bootstrapTable是total
private List<T> data; // 当前数据, bootstrapTable是rows
public PageResult() {
}
public PageResult(List<T> rows) {
this(rows, rows.size());
}
public PageResult(List<T> rows, long total) {
this.count = total;
this.data = rows;
this.code = 0;
this.msg = "";
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
}