Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
武汉红喜
whatsmars
提交
3f08f6a1
W
whatsmars
项目概览
武汉红喜
/
whatsmars
通知
3
Star
0
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
W
whatsmars
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
3f08f6a1
编写于
12月 21, 2017
作者:
武汉红喜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
AppExceptionHandler
上级
78c91c7f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
16 addition
and
16 deletion
+16
-16
whatsmars-spring-boot/src/main/java/com/whatsmars/spring/boot/controller/NewController.java
...a/com/whatsmars/spring/boot/controller/NewController.java
+2
-2
whatsmars-spring-boot/src/main/java/com/whatsmars/spring/boot/exception/AppException.java
...ava/com/whatsmars/spring/boot/exception/AppException.java
+10
-10
whatsmars-spring-boot/src/main/java/com/whatsmars/spring/boot/exception/AppExceptionHandler.java
.../whatsmars/spring/boot/exception/AppExceptionHandler.java
+4
-4
未找到文件。
whatsmars-spring-boot/src/main/java/com/whatsmars/spring/boot/controller/NewController.java
浏览文件 @
3f08f6a1
package
com.whatsmars.spring.boot.controller
;
import
com.whatsmars.spring.boot.exception.
Business
Exception
;
import
com.whatsmars.spring.boot.exception.
App
Exception
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
...
...
@@ -47,7 +47,7 @@ public class NewController {
@RequestMapping
(
value
=
"/e"
,
method
=
RequestMethod
.
GET
)
public
HttpStatus
e
(
@RequestParam
(
required
=
false
)
String
error
)
{
if
(
error
!=
null
)
throw
Business
Exception
.
build
(
error
);
if
(
error
!=
null
)
throw
App
Exception
.
build
(
error
);
return
HttpStatus
.
OK
;
}
}
whatsmars-spring-boot/src/main/java/com/whatsmars/spring/boot/exception/
Business
Exception.java
→
whatsmars-spring-boot/src/main/java/com/whatsmars/spring/boot/exception/
App
Exception.java
浏览文件 @
3f08f6a1
...
...
@@ -7,12 +7,12 @@ import org.springframework.validation.ObjectError;
/**
* Created by shenhongxi on 2017/11/16.
*/
public
class
Business
Exception
extends
RuntimeException
{
public
class
App
Exception
extends
RuntimeException
{
private
Code
errorCode
;
private
String
errorMsg
;
private
Business
Exception
()
{
private
App
Exception
()
{
}
@Override
...
...
@@ -21,27 +21,27 @@ public class BusinessException extends RuntimeException {
return
this
.
getErrorMsg
();
}
public
static
Business
Exception
build
(
Code
errorCode
,
String
errorMsg
)
{
return
new
Business
Exception
().
setErrorCode
(
errorCode
).
setErrorMsg
(
errorMsg
);
public
static
App
Exception
build
(
Code
errorCode
,
String
errorMsg
)
{
return
new
App
Exception
().
setErrorCode
(
errorCode
).
setErrorMsg
(
errorMsg
);
}
public
static
Business
Exception
build
(
String
errorMsg
)
{
return
new
Business
Exception
().
setErrorCode
(
Code
.
ERROR
).
setErrorMsg
(
errorMsg
);
public
static
App
Exception
build
(
String
errorMsg
)
{
return
new
App
Exception
().
setErrorCode
(
Code
.
ERROR
).
setErrorMsg
(
errorMsg
);
}
public
static
Business
Exception
build
(
BindingResult
bindingResult
)
{
public
static
App
Exception
build
(
BindingResult
bindingResult
)
{
StringBuilder
sb
=
new
StringBuilder
();
for
(
ObjectError
e
:
bindingResult
.
getAllErrors
())
{
sb
.
append
(
e
.
getDefaultMessage
()).
append
(
";"
);
}
return
new
Business
Exception
().
setErrorCode
(
Code
.
ERROR
).
setErrorMsg
(
sb
.
toString
());
return
new
App
Exception
().
setErrorCode
(
Code
.
ERROR
).
setErrorMsg
(
sb
.
toString
());
}
public
Code
getErrorCode
()
{
return
errorCode
;
}
public
Business
Exception
setErrorCode
(
Code
errorCode
)
{
public
App
Exception
setErrorCode
(
Code
errorCode
)
{
this
.
errorCode
=
errorCode
;
return
this
;
}
...
...
@@ -50,7 +50,7 @@ public class BusinessException extends RuntimeException {
return
errorMsg
;
}
public
Business
Exception
setErrorMsg
(
String
errorMsg
)
{
public
App
Exception
setErrorMsg
(
String
errorMsg
)
{
this
.
errorMsg
=
errorMsg
;
return
this
;
}
...
...
whatsmars-spring-boot/src/main/java/com/whatsmars/spring/boot/exception/
Business
ExceptionHandler.java
→
whatsmars-spring-boot/src/main/java/com/whatsmars/spring/boot/exception/
App
ExceptionHandler.java
浏览文件 @
3f08f6a1
...
...
@@ -23,15 +23,15 @@ import java.util.Map;
* Created by shenhongxi on 2017/11/16.
*/
@ControllerAdvice
(
annotations
=
{
RestController
.
class
,
Controller
.
class
})
public
class
Business
ExceptionHandler
{
public
class
App
ExceptionHandler
{
private
Logger
logger
=
LoggerFactory
.
getLogger
(
getClass
());
/**
* 业务异常处理,直接返回异常信息提示
*/
@ExceptionHandler
(
Business
Exception
.
class
)
public
ResponseEntity
<
ReturnItem
>
businessExceptionHandle
(
Business
Exception
exception
,
HttpServletRequest
request
)
{
@ExceptionHandler
(
App
Exception
.
class
)
public
ResponseEntity
<
ReturnItem
>
businessExceptionHandle
(
App
Exception
exception
,
HttpServletRequest
request
)
{
logError
(
exception
,
request
,
LogLevel
.
WARN
);
return
new
ResponseEntity
<
ReturnItem
>(
exception
.
toResultItem
(),
HttpStatus
.
OK
);
}
...
...
@@ -42,7 +42,7 @@ public class BusinessExceptionHandler {
@ExceptionHandler
(
Exception
.
class
)
public
ResponseEntity
<
ReturnItem
>
defaultExceptionHandle
(
Exception
exception
,
HttpServletRequest
request
)
{
logError
(
exception
,
request
,
LogLevel
.
ERROR
);
return
new
ResponseEntity
<
ReturnItem
>(
new
ReturnItem
(
Business
Exception
.
Code
.
ERROR
.
getValue
(),
ProfileUtils
.
isDev
()
?
exception
.
getMessage
()
:
ReturnMessage
.
Message
.
OPERATION_ERROR
.
getValue
()),
HttpStatus
.
OK
);
return
new
ResponseEntity
<
ReturnItem
>(
new
ReturnItem
(
App
Exception
.
Code
.
ERROR
.
getValue
(),
ProfileUtils
.
isDev
()
?
exception
.
getMessage
()
:
ReturnMessage
.
Message
.
OPERATION_ERROR
.
getValue
()),
HttpStatus
.
OK
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录