Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
haoban-manage3.0
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
haoban3.0
haoban-manage3.0
Commits
f5fd2e61
Commit
f5fd2e61
authored
Dec 15, 2021
by
墨竹
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:拦截器修改
parent
46c2833f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
136 additions
and
59 deletions
+136
-59
GlobalExceptionHandler.java
...c/haoban/manage/web/exception/GlobalExceptionHandler.java
+75
-16
ControllerException.java
.../gic/haoban/manage/web/exception/ControllerException.java
+0
-10
GlobalExceptionHandler.java
...c/haoban/manage/web/exception/GlobalExceptionHandler.java
+61
-33
No files found.
haoban-manage3-web/src/main/java/com/gic/haoban/manage/web/exception/GlobalExceptionHandler.java
View file @
f5fd2e61
...
@@ -6,6 +6,10 @@ import com.gic.haoban.manage.web.controller.WebBaseController;
...
@@ -6,6 +6,10 @@ import com.gic.haoban.manage.web.controller.WebBaseController;
import
com.gic.haoban.manage.web.errCode.HaoBanErrCode
;
import
com.gic.haoban.manage.web.errCode.HaoBanErrCode
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.validation.FieldError
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseBody
;
...
@@ -23,17 +27,22 @@ import java.util.regex.Pattern;
...
@@ -23,17 +27,22 @@ import java.util.regex.Pattern;
*/
*/
@ControllerAdvice
@ControllerAdvice
public
class
GlobalExceptionHandler
extends
WebBaseController
{
public
class
GlobalExceptionHandler
extends
WebBaseController
{
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
GlobalExceptionHandler
.
class
);
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
GlobalExceptionHandler
.
class
);
private
static
final
Pattern
pattern
=
Pattern
.
compile
(
"(.*: )(.*)(\\n.*)"
);
private
static
final
Pattern
pattern
=
Pattern
.
compile
(
"(.*: )(.*)(\\n.*)"
);
/**
* 控制器运行异常
*
* @param response 响应
* @param ex 前女友
* @return {@link HaobanResponse }
* @author mozhu
* @date 2021-12-15 14:55:50
*/
@ResponseBody
@ResponseBody
@ExceptionHandler
(
RuntimeException
.
class
)
@ExceptionHandler
(
RuntimeException
.
class
)
public
HaobanResponse
ControllerException
(
HttpServletResponse
response
,
RuntimeException
ex
)
{
public
HaobanResponse
controllerRunException
(
HttpServletResponse
response
,
RuntimeException
ex
)
{
logger
.
info
(
"exceoprion:{}"
,
ex
.
getStackTrace
());
logger
.
error
(
"controllerRunException:{}"
,
ex
.
getMessage
(),
ex
);
ex
.
printStackTrace
();
HaobanResponse
res
=
new
HaobanResponse
();
HaobanResponse
res
=
new
HaobanResponse
();
res
.
setErrorCode
(
HaoBanErrCode
.
ERR_3
.
getCode
());
res
.
setErrorCode
(
HaoBanErrCode
.
ERR_3
.
getCode
());
String
message
=
ex
.
getMessage
();
String
message
=
ex
.
getMessage
();
...
@@ -45,16 +54,23 @@ public class GlobalExceptionHandler extends WebBaseController {
...
@@ -45,16 +54,23 @@ public class GlobalExceptionHandler extends WebBaseController {
}
else
{
}
else
{
message
=
HaoBanErrCode
.
ERR_3
.
getMsg
();
message
=
HaoBanErrCode
.
ERR_3
.
getMsg
();
}
}
res
.
setMessage
(
"系统异常"
);
res
.
setMessage
(
message
);
return
res
;
return
res
;
}
}
/**
* 全局异常
*
* @param response 响应
* @param ex
* @return {@link HaobanResponse }
* @author mozhu
* @date 2021-12-15 14:54:25
*/
@ResponseBody
@ResponseBody
@ExceptionHandler
(
Exception
.
class
)
@ExceptionHandler
(
Exception
.
class
)
public
HaobanResponse
ControllerException
(
HttpServletResponse
response
,
Exception
ex
)
{
public
HaobanResponse
controllerException
(
HttpServletResponse
response
,
Exception
ex
)
{
logger
.
error
(
"err"
);
logger
.
error
(
"controllerException:{}"
,
ex
.
getMessage
(),
ex
);
ex
.
printStackTrace
();
HaoBanErrCode
errCode
=
HaoBanErrCode
.
ERR_3
;
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
try
(
PrintWriter
printWriter
=
new
PrintWriter
(
baos
))
{
try
(
PrintWriter
printWriter
=
new
PrintWriter
(
baos
))
{
...
@@ -67,16 +83,59 @@ public class GlobalExceptionHandler extends WebBaseController {
...
@@ -67,16 +83,59 @@ public class GlobalExceptionHandler extends WebBaseController {
if
(
sb
.
length
()
==
0
)
{
if
(
sb
.
length
()
==
0
)
{
sb
.
append
(
ex
.
getMessage
());
sb
.
append
(
ex
.
getMessage
());
}
}
// 输出详细错误信息,便于调试
// return resultResponse(errCode, null, sb.toString());
return
resultResponse
(
HaoBanErrCode
.
ERR_3
);
return
resultResponse
(
HaoBanErrCode
.
ERR_3
);
}
}
/**
* valid 异常告警
*
* @param response
* @param ex
* @return {@link HaobanResponse }
* @author mozhu
* @date 2021-12-15 14:55:40
*/
@ResponseBody
@ExceptionHandler
(
BindException
.
class
)
public
HaobanResponse
bindException
(
HttpServletResponse
response
,
BindException
ex
)
{
logger
.
error
(
"bindException:{}"
,
ex
.
getMessage
());
HaoBanErrCode
errCode
=
HaoBanErrCode
.
ERR_5
;
BindingResult
bindingResult
=
ex
.
getBindingResult
();
FieldError
fieldError
=
bindingResult
.
getFieldError
();
return
resultResponse
(
errCode
,
null
,
fieldError
.
getField
()
+
fieldError
.
getDefaultMessage
());
}
/**
* valid 异常告警 @ReuqestBody
*
* @param response
* @param ex
* @return {@link HaobanResponse }
* @author mozhu
* @date 2021-12-15 14:55:35
*/
@ResponseBody
@ExceptionHandler
(
MethodArgumentNotValidException
.
class
)
public
HaobanResponse
methodArgumentNotValidException
(
HttpServletResponse
response
,
MethodArgumentNotValidException
ex
)
{
logger
.
error
(
"methodArgumentNotValidException:{}"
,
ex
.
getMessage
());
HaoBanErrCode
errCode
=
HaoBanErrCode
.
ERR_5
;
BindingResult
bindingResult
=
ex
.
getBindingResult
();
FieldError
fieldError
=
bindingResult
.
getFieldError
();
return
resultResponse
(
errCode
,
null
,
fieldError
.
getField
()
+
fieldError
.
getDefaultMessage
());
}
/**
* 登录异常
*
* @param response 响应
* @param ex
* @return {@link HaobanResponse }
* @author mozhu
* @date 2021-12-15 14:55:16
*/
@ResponseBody
@ResponseBody
@ExceptionHandler
(
NoLoginException
.
class
)
@ExceptionHandler
(
NoLoginException
.
class
)
public
HaobanResponse
Controller
Exception
(
HttpServletResponse
response
,
NoLoginException
ex
)
{
public
HaobanResponse
controllerNoLogin
Exception
(
HttpServletResponse
response
,
NoLoginException
ex
)
{
return
resultResponse
(
HaoBanErrCode
.
ERR_4
);
return
resultResponse
(
HaoBanErrCode
.
ERR_4
);
}
}
...
...
haoban-manage3-wx/src/main/java/com/gic/haoban/manage/web/exception/ControllerException.java
deleted
100644 → 0
View file @
46c2833f
package
com
.
gic
.
haoban
.
manage
.
web
.
exception
;
/**
* Created 2018/7/24.
*
* @author hua
*/
public
class
ControllerException
extends
Exception
{
}
haoban-manage3-wx/src/main/java/com/gic/haoban/manage/web/exception/GlobalExceptionHandler.java
View file @
f5fd2e61
package
com
.
gic
.
haoban
.
manage
.
web
.
exception
;
package
com
.
gic
.
haoban
.
manage
.
web
.
exception
;
import
com.gic.haoban.base.api.common.ShowFrontMessageException
;
import
com.gic.haoban.common.utils.HaobanResponse
;
import
com.gic.haoban.common.utils.HaobanResponse
;
import
com.gic.haoban.manage.web.controller.WebBaseController
;
import
com.gic.haoban.manage.web.controller.WebBaseController
;
import
com.gic.haoban.manage.web.errCode.HaoBanErrCode
;
import
com.gic.haoban.manage.web.errCode.HaoBanErrCode
;
...
@@ -16,6 +17,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
...
@@ -16,6 +17,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.ByteArrayOutputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.PrintWriter
;
import
java.io.PrintWriter
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
/**
/**
* Created 2018/7/24.
* Created 2018/7/24.
...
@@ -25,19 +28,49 @@ import java.io.PrintWriter;
...
@@ -25,19 +28,49 @@ import java.io.PrintWriter;
@ControllerAdvice
@ControllerAdvice
public
class
GlobalExceptionHandler
extends
WebBaseController
{
public
class
GlobalExceptionHandler
extends
WebBaseController
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
GlobalExceptionHandler
.
class
);
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
GlobalExceptionHandler
.
class
);
private
static
final
Pattern
pattern
=
Pattern
.
compile
(
"(.*: )(.*)(\\n.*)"
);
/**
/**
*
RuntimeException 类型异常告警
*
控制器运行异常
*
*
* @param response
* @param response 响应
* @param ex
* @param ex 前女友
* @return
* @return {@link HaobanResponse }
* @author mozhu
* @date 2021-12-15 14:55:50
*/
*/
@ResponseBody
@ResponseBody
@ExceptionHandler
(
RuntimeException
.
class
)
@ExceptionHandler
(
RuntimeException
.
class
)
public
HaobanResponse
controllerException
(
HttpServletResponse
response
,
RuntimeException
ex
)
{
public
HaobanResponse
controllerRunException
(
HttpServletResponse
response
,
RuntimeException
ex
)
{
logger
.
error
(
"Exception:{}"
,
ex
.
getMessage
(),
ex
);
logger
.
error
(
"controllerRunException:{}"
,
ex
.
getMessage
(),
ex
);
HaoBanErrCode
errCode
=
HaoBanErrCode
.
ERR_3
;
HaobanResponse
res
=
new
HaobanResponse
();
res
.
setErrorCode
(
HaoBanErrCode
.
ERR_3
.
getCode
());
String
message
=
ex
.
getMessage
();
if
(
message
!=
null
&&
message
.
indexOf
(
ShowFrontMessageException
.
class
.
getName
())
>
-
1
)
{
Matcher
matcher
=
pattern
.
matcher
(
message
);
while
(
matcher
.
find
())
{
message
=
(
matcher
.
group
(
2
));
}
}
else
{
message
=
HaoBanErrCode
.
ERR_3
.
getMsg
();
}
res
.
setMessage
(
message
);
return
res
;
}
/**
* 全局异常
*
* @param response 响应
* @param ex
* @return {@link HaobanResponse }
* @author mozhu
* @date 2021-12-15 14:54:25
*/
@ResponseBody
@ExceptionHandler
(
Exception
.
class
)
public
HaobanResponse
controllerException
(
HttpServletResponse
response
,
Exception
ex
)
{
logger
.
error
(
"controllerException:{}"
,
ex
.
getMessage
(),
ex
);
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
try
(
PrintWriter
printWriter
=
new
PrintWriter
(
baos
))
{
try
(
PrintWriter
printWriter
=
new
PrintWriter
(
baos
))
{
...
@@ -50,8 +83,7 @@ public class GlobalExceptionHandler extends WebBaseController {
...
@@ -50,8 +83,7 @@ public class GlobalExceptionHandler extends WebBaseController {
if
(
sb
.
length
()
==
0
)
{
if
(
sb
.
length
()
==
0
)
{
sb
.
append
(
ex
.
getMessage
());
sb
.
append
(
ex
.
getMessage
());
}
}
// 输出详细错误信息,便于调试
return
resultResponse
(
HaoBanErrCode
.
ERR_3
);
return
resultResponse
(
errCode
,
null
,
sb
.
toString
());
}
}
/**
/**
...
@@ -59,12 +91,14 @@ public class GlobalExceptionHandler extends WebBaseController {
...
@@ -59,12 +91,14 @@ public class GlobalExceptionHandler extends WebBaseController {
*
*
* @param response
* @param response
* @param ex
* @param ex
* @return
* @return {@link HaobanResponse }
* @author mozhu
* @date 2021-12-15 14:55:40
*/
*/
@ResponseBody
@ResponseBody
@ExceptionHandler
(
BindException
.
class
)
@ExceptionHandler
(
BindException
.
class
)
public
HaobanResponse
bindException
(
HttpServletResponse
response
,
BindException
ex
)
{
public
HaobanResponse
bindException
(
HttpServletResponse
response
,
BindException
ex
)
{
logger
.
error
(
"
Exception:{}"
,
ex
.
getMessage
(),
ex
);
logger
.
error
(
"
bindException:{}"
,
ex
.
getMessage
()
);
HaoBanErrCode
errCode
=
HaoBanErrCode
.
ERR_5
;
HaoBanErrCode
errCode
=
HaoBanErrCode
.
ERR_5
;
BindingResult
bindingResult
=
ex
.
getBindingResult
();
BindingResult
bindingResult
=
ex
.
getBindingResult
();
FieldError
fieldError
=
bindingResult
.
getFieldError
();
FieldError
fieldError
=
bindingResult
.
getFieldError
();
...
@@ -76,38 +110,32 @@ public class GlobalExceptionHandler extends WebBaseController {
...
@@ -76,38 +110,32 @@ public class GlobalExceptionHandler extends WebBaseController {
*
*
* @param response
* @param response
* @param ex
* @param ex
* @return
* @return {@link HaobanResponse }
* @author mozhu
* @date 2021-12-15 14:55:35
*/
*/
@ResponseBody
@ResponseBody
@ExceptionHandler
(
MethodArgumentNotValidException
.
class
)
@ExceptionHandler
(
MethodArgumentNotValidException
.
class
)
public
HaobanResponse
methodArgumentNotValidException
(
HttpServletResponse
response
,
MethodArgumentNotValidException
ex
)
{
public
HaobanResponse
methodArgumentNotValidException
(
HttpServletResponse
response
,
MethodArgumentNotValidException
ex
)
{
logger
.
error
(
"
Exception:{}"
,
ex
.
getMessage
(),
ex
);
logger
.
error
(
"
methodArgumentNotValidException:{}"
,
ex
.
getMessage
()
);
HaoBanErrCode
errCode
=
HaoBanErrCode
.
ERR_5
;
HaoBanErrCode
errCode
=
HaoBanErrCode
.
ERR_5
;
BindingResult
bindingResult
=
ex
.
getBindingResult
();
BindingResult
bindingResult
=
ex
.
getBindingResult
();
FieldError
fieldError
=
bindingResult
.
getFieldError
();
FieldError
fieldError
=
bindingResult
.
getFieldError
();
return
resultResponse
(
errCode
,
null
,
fieldError
.
getField
()
+
fieldError
.
getDefaultMessage
());
return
resultResponse
(
errCode
,
null
,
fieldError
.
getField
()
+
fieldError
.
getDefaultMessage
());
}
}
/**
* 登录异常
*
* @param response 响应
* @param ex
* @return {@link HaobanResponse }
* @author mozhu
* @date 2021-12-15 14:55:16
*/
@ResponseBody
@ResponseBody
@ExceptionHandler
(
Exception
.
class
)
@ExceptionHandler
(
NoLoginException
.
class
)
public
HaobanResponse
globalException
(
HttpServletResponse
response
,
Exception
ex
)
{
public
HaobanResponse
controllerNoLoginException
(
HttpServletResponse
response
,
NoLoginException
ex
)
{
logger
.
error
(
"Exception:{}"
,
ex
.
getMessage
(),
ex
);
return
resultResponse
(
HaoBanErrCode
.
ERR_4
);
HaoBanErrCode
errCode
=
HaoBanErrCode
.
ERR_3
;
StringBuilder
sb
=
new
StringBuilder
();
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
try
(
PrintWriter
printWriter
=
new
PrintWriter
(
baos
))
{
ex
.
printStackTrace
(
printWriter
);
}
try
{
sb
.
append
(
baos
.
toString
());
}
catch
(
Exception
ignored
)
{
}
if
(
sb
.
length
()
==
0
)
{
sb
.
append
(
ex
.
getMessage
());
}
// 输出详细错误信息,便于调试
return
resultResponse
(
errCode
,
null
,
sb
.
toString
());
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment