Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
cxt104926
Stusystem
提交
d65b4329
S
Stusystem
项目概览
cxt104926
/
Stusystem
通知
7
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Stusystem
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d65b4329
编写于
3月 10, 2021
作者:
cxt104926
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1
上级
f89180ca
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
207 addition
and
4 deletion
+207
-4
src/main/java/com/stu/stusystem/common/ApiResult.java
src/main/java/com/stu/stusystem/common/ApiResult.java
+12
-4
src/main/java/com/stu/stusystem/exception/BaseErrorInfoInterface.java
...a/com/stu/stusystem/exception/BaseErrorInfoInterface.java
+16
-0
src/main/java/com/stu/stusystem/exception/BizException.java
src/main/java/com/stu/stusystem/exception/BizException.java
+80
-0
src/main/java/com/stu/stusystem/exception/CommonEnum.java
src/main/java/com/stu/stusystem/exception/CommonEnum.java
+40
-0
src/main/java/com/stu/stusystem/exception/GlobalExceptionHandler.java
...a/com/stu/stusystem/exception/GlobalExceptionHandler.java
+59
-0
未找到文件。
src/main/java/com/stu/stusystem/common/ApiResult.java
浏览文件 @
d65b4329
package
com.stu.stusystem.common
;
import
com.stu.stusystem.exception.CommonEnum
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.*
;
...
...
@@ -25,7 +26,7 @@ public class ApiResult<T> implements Serializable {
private
Boolean
success
;
@ApiModelProperty
(
value
=
"状态码"
,
required
=
true
)
private
int
code
;
private
Integer
code
;
@ApiModelProperty
(
value
=
"消息"
,
required
=
true
)
private
String
message
;
...
...
@@ -33,13 +34,13 @@ public class ApiResult<T> implements Serializable {
@ApiModelProperty
(
value
=
"返回数据"
)
private
T
data
;
public
ApiResult
(
Boolean
success
,
int
code
,
String
message
)
{
public
ApiResult
(
Boolean
success
,
Integer
code
,
String
message
)
{
this
.
success
=
success
;
this
.
code
=
code
;
this
.
message
=
message
;
}
public
ApiResult
(
int
code
,
String
message
)
{
public
ApiResult
(
Integer
code
,
String
message
)
{
this
.
success
=
false
;
this
.
code
=
code
;
this
.
message
=
message
;
...
...
@@ -104,7 +105,14 @@ public class ApiResult<T> implements Serializable {
/**
* 错误
*/
public
static
<
T
>
ApiResult
<
T
>
error
(
int
code
,
String
message
)
{
public
static
<
T
>
ApiResult
<
T
>
error
(
CommonEnum
e
)
{
return
new
ApiResult
<
T
>(
false
,
500
,
e
.
getResultMsg
());
}
/**
* 错误
*/
public
static
<
T
>
ApiResult
<
T
>
error
(
Integer
code
,
String
message
)
{
return
new
ApiResult
<
T
>(
false
,
code
,
message
);
}
}
src/main/java/com/stu/stusystem/exception/BaseErrorInfoInterface.java
0 → 100644
浏览文件 @
d65b4329
package
com.stu.stusystem.exception
;
/**
* 异常信息接口
*/
public
interface
BaseErrorInfoInterface
{
/**
* 错误码
*/
Integer
getResultCode
();
/**
* 错误描述
*/
String
getResultMsg
();
}
\ No newline at end of file
src/main/java/com/stu/stusystem/exception/BizException.java
0 → 100644
浏览文件 @
d65b4329
package
com.stu.stusystem.exception
;
/**
* @author: cxt
* @time: 2021/3/10
* 自定义异常类
*/
public
class
BizException
extends
RuntimeException
{
private
static
final
long
serialVersionUID
=
1L
;
/**
* 错误码
*/
protected
String
errorCode
;
/**
* 错误信息
*/
protected
String
errorMsg
;
public
BizException
()
{
super
();
}
public
BizException
(
BaseErrorInfoInterface
errorInfoInterface
)
{
super
(
errorInfoInterface
.
getResultCode
());
this
.
errorCode
=
errorInfoInterface
.
getResultCode
();
this
.
errorMsg
=
errorInfoInterface
.
getResultMsg
();
}
public
BizException
(
BaseErrorInfoInterface
errorInfoInterface
,
Throwable
cause
)
{
super
(
errorInfoInterface
.
getResultCode
(),
cause
);
this
.
errorCode
=
errorInfoInterface
.
getResultCode
();
this
.
errorMsg
=
errorInfoInterface
.
getResultMsg
();
}
public
BizException
(
String
errorMsg
)
{
super
(
errorMsg
);
this
.
errorMsg
=
errorMsg
;
}
public
BizException
(
String
errorCode
,
String
errorMsg
)
{
super
(
errorCode
);
this
.
errorCode
=
errorCode
;
this
.
errorMsg
=
errorMsg
;
}
public
BizException
(
String
errorCode
,
String
errorMsg
,
Throwable
cause
)
{
super
(
errorCode
,
cause
);
this
.
errorCode
=
errorCode
;
this
.
errorMsg
=
errorMsg
;
}
public
String
getErrorCode
()
{
return
errorCode
;
}
public
void
setErrorCode
(
String
errorCode
)
{
this
.
errorCode
=
errorCode
;
}
public
String
getErrorMsg
()
{
return
errorMsg
;
}
public
void
setErrorMsg
(
String
errorMsg
)
{
this
.
errorMsg
=
errorMsg
;
}
public
String
getMessage
()
{
return
errorMsg
;
}
@Override
public
Throwable
fillInStackTrace
()
{
return
this
;
}
}
\ No newline at end of file
src/main/java/com/stu/stusystem/exception/CommonEnum.java
0 → 100644
浏览文件 @
d65b4329
package
com.stu.stusystem.exception
;
/**
* 异常枚举类
*/
public
enum
CommonEnum
implements
BaseErrorInfoInterface
{
// 数据操作错误定义
SUCCESS
(
"200"
,
"成功!"
),
BODY_NOT_MATCH
(
"400"
,
"请求的数据格式不符!"
),
SIGNATURE_NOT_MATCH
(
"401"
,
"请求的数字签名不匹配!"
),
NOT_FOUND
(
"404"
,
"未找到该资源!"
),
INTERNAL_SERVER_ERROR
(
"500"
,
"服务器内部错误!"
),
SERVER_BUSY
(
"503"
,
"服务器正忙,请稍后再试!"
);
/**
* 错误码
*/
private
String
resultCode
;
/**
* 错误描述
*/
private
String
resultMsg
;
CommonEnum
(
String
resultCode
,
String
resultMsg
)
{
this
.
resultCode
=
resultCode
;
this
.
resultMsg
=
resultMsg
;
}
@Override
public
String
getResultCode
()
{
return
resultCode
;
}
@Override
public
String
getResultMsg
()
{
return
resultMsg
;
}
}
src/main/java/com/stu/stusystem/exception/GlobalExceptionHandler.java
0 → 100644
浏览文件 @
d65b4329
package
com.stu.stusystem.exception
;
import
com.stu.stusystem.common.ApiResult
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
javax.servlet.http.HttpServletRequest
;
/**
* @author: cxt
* @time: 2021/3/10
* 全局异常处理
*/
@ControllerAdvice
@Slf4j
public
class
GlobalExceptionHandler
{
/**
* 处理自定义的业务异常
* @param req
* @param e
* @return
*/
@ExceptionHandler
(
value
=
BizException
.
class
)
@ResponseBody
public
ApiResult
bizExceptionHandler
(
HttpServletRequest
req
,
BizException
e
){
log
.
error
(
"发生业务异常!原因是:{}"
,
e
.
getErrorMsg
());
return
ApiResult
.
error
(
Integer
.
parseInt
(
e
.
getErrorCode
()),
e
.
getErrorMsg
());
}
/**
* 处理空指针的异常
* @param req
* @param e
* @return
*/
@ExceptionHandler
(
value
=
NullPointerException
.
class
)
@ResponseBody
public
ApiResult
exceptionHandler
(
HttpServletRequest
req
,
NullPointerException
e
){
log
.
error
(
"发生空指针异常!原因是:"
,
e
);
return
ApiResult
.
error
(
CommonEnum
.
BODY_NOT_MATCH
);
}
/**
* 处理其他异常
* @param req
* @param e
* @return
*/
@ExceptionHandler
(
value
=
Exception
.
class
)
@ResponseBody
public
ApiResult
exceptionHandler
(
HttpServletRequest
req
,
Exception
e
){
log
.
error
(
"未知异常!原因是:"
,
e
);
return
ApiResult
.
error
(
CommonEnum
.
INTERNAL_SERVER_ERROR
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录