优化操作消息

This commit is contained in:
RuoYi
2018-04-27 13:29:57 +08:00
parent 1788566a63
commit 21a743056c
17 changed files with 262 additions and 191 deletions

View File

@ -1,64 +0,0 @@
package com.ruoyi.framework.web.domain;
import java.util.HashMap;
import java.util.Map;
/**
* 返回数据通用处理
*
* @author ruoyi
*/
public class JSON extends HashMap<String, Object>
{
private static final long serialVersionUID = 1L;
public JSON()
{
put("code", 0);
put("msg", "操作成功");
}
public static JSON error()
{
return error(1, "操作失败");
}
public static JSON error(String msg)
{
return error(500, msg);
}
public static JSON error(int code, String msg)
{
JSON json = new JSON();
json.put("code", code);
json.put("msg", msg);
return json;
}
public static JSON ok(String msg)
{
JSON json = new JSON();
json.put("msg", msg);
return json;
}
public static JSON ok(Map<String, Object> map)
{
JSON json = new JSON();
json.putAll(map);
return json;
}
public static JSON ok()
{
return new JSON();
}
@Override
public JSON put(String key, Object value)
{
super.put(key, value);
return this;
}
}

View File

@ -0,0 +1,109 @@
package com.ruoyi.framework.web.domain;
import java.util.HashMap;
import java.util.Map;
/**
* 操作消息提醒
*
* @author ruoyi
*/
public class Message extends HashMap<String, Object>
{
private static final long serialVersionUID = 1L;
/**
* 初始化一个新创建的 Message 对象,默认成功。
*/
public Message()
{
put("code", 0);
put("msg", "操作成功");
}
/**
* 返回错误消息
*
* @return 错误消息
*/
public static Message error()
{
return error(1, "操作失败");
}
/**
* 返回错误消息
*
* @param msg 内容
* @return 错误消息
*/
public static Message error(String msg)
{
return error(500, msg);
}
/**
* 返回错误消息
*
* @param code 错误码
* @param msg 内容
* @return 错误消息
*/
public static Message error(int code, String msg)
{
Message json = new Message();
json.put("code", code);
json.put("msg", msg);
return json;
}
/**
* 返回成功消息
*
* @param msg 内容
* @return 成功消息
*/
public static Message ok(String msg)
{
Message json = new Message();
json.put("msg", msg);
return json;
}
/**
* 返回成功消息
*
* @param map 内容
* @return 成功消息
*/
public static Message ok(Map<String, Object> map)
{
Message json = new Message();
json.putAll(map);
return json;
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static Message ok()
{
return new Message();
}
/**
* 返回成功消息
*
* @param key 键值
* @param value 内容
* @return 成功消息
*/
@Override
public Message put(String key, Object value)
{
super.put(key, value);
return this;
}
}

View File

@ -3,10 +3,12 @@ package com.ruoyi.framework.web.exception;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import com.ruoyi.framework.web.domain.JSON;
import com.ruoyi.common.exception.DemoModeException;
import com.ruoyi.framework.web.domain.Message;
/**
* 自定义异常处理器
@ -22,40 +24,49 @@ public class DefaultExceptionHandler
* 权限校验失败
*/
@ExceptionHandler(AuthorizationException.class)
public JSON handleAuthorizationException(AuthorizationException e)
public Message handleAuthorizationException(AuthorizationException e)
{
log.error(e.getMessage(), e);
return JSON.error("您没有数据的权限,请联系管理员添加");
return Message.error("您没有数据的权限,请联系管理员添加");
}
/**
* 请求方式不支持
*/
@ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
public JSON handleException(HttpRequestMethodNotSupportedException e)
public Message handleException(HttpRequestMethodNotSupportedException e)
{
log.error(e.getMessage(), e);
return JSON.error("不支持' " + e.getMethod() + "'请求");
return Message.error("不支持' " + e.getMethod() + "'请求");
}
/**
* 拦截未知的运行时异常
*/
@ExceptionHandler(RuntimeException.class)
public JSON notFount(RuntimeException e)
public Message notFount(RuntimeException e)
{
log.error("运行时异常:", e);
return JSON.error("运行时异常:" + e.getMessage());
return Message.error("运行时异常:" + e.getMessage());
}
/**
* 系统异常
*/
@ExceptionHandler(Exception.class)
public JSON handleException(Exception e)
public Message handleException(Exception e)
{
log.error(e.getMessage(), e);
return JSON.error("服务器错误,请联系管理员");
return Message.error("服务器错误,请联系管理员");
}
/**
* 演示模式异常
*/
@ExceptionHandler(DemoModeException.class)
public Message demoModeException(DemoModeException e)
{
return Message.error("演示模式,不允许操作");
}
}