SpringBoot全局异常处理

20 篇文章 0 订阅
20 篇文章 0 订阅

在这里插入图片描述
简介

通常在Controller层需要去捕获service层的异常,防止返回一些不友好的错误信息到客户端,但如果Controller层每个方法都用模块化的try-catch代码去捕获异常,会很难看也难维护,所以使用全局异常比较方便

这方法是springboot封装好了的,我们直接使用即可,普通的配置我就不贴了

使用

配置类

代码示例

/**

  • @program:hope
  • @author:aodeng
  • @create:2018-10-10 14:15
    **/
    @RestControllerAdvice

//该注解将异常以json格式输出
public

class

GlobalExceptionHandler
extends
ResponseEntityExceptionHandler
{

/***
*定义要普获的异常

  • @param request
  • @param e
  • @param response
  • @return
    */

@ExceptionHandler
(
CustomException
.
class
)

public

ExceptionEntity
customExceptionEntity
(
HttpServletRequest
request
,
final
Exception
e
,

HttpServletResponse
response
){
response
.
setStatus
(
HttpStatus
.
BAD_REQUEST
.
value
());

CustomException
customException
=(
CustomException
)
e
;

return

new

ExceptionEntity
(
customException
.
getCode
(),
customException
.
getMessage
());

}

/***

  • 捕获 RuntimeException 异常
  • 如果你觉得在一个 exceptionHandler 通过 if (e instanceof xxxException) 太麻烦
  • 那么你还可以自己写多个不同的 exceptionHandler 处理不同异常
  • @param request
  • @param e
  • @param response
  • @return
    */

@ExceptionHandler
(
RuntimeException
.
class
)

public

ExceptionEntity
runtimeExceptionEntity
(
HttpServletRequest
request
,
final
Exception
e
,
HttpServletResponse
response
){
response
.
setStatus
(
HttpStatus
.
BAD_REQUEST
.
value
());

RuntimeException
runtimeException
=(
RuntimeException
)
e
;

return

new

ExceptionEntity
(
400
,
runtimeException
.
getMessage
());

}

/***

  • Override ResponseEntityExceptionHandler类中的handleExceptionInternal方法
  • 通用的接口映射异常处理方
  • @param e
  • @param o
  • @param httpHeaders
  • @param httpStatus
  • @param webRequest
  • @return
    */

@Override

protected

ResponseEntity
<
Object

handleExceptionInternal
(
Exception
e
,

Object
o
,

HttpHeaders
httpHeaders
,

HttpStatus
httpStatus
,

WebRequest
webRequest
){

if

(
e
instanceof

MethodArgumentNotValidException
){

MethodArgumentNotValidException
methodArgumentNotValidException
=(
MethodArgumentNotValidException
)
e
;

return

new

ResponseEntity
<>(
new

ExceptionEntity
(
httpStatus
.
value
(),
methodArgumentNotValidException
.
getBindingResult
().
getAllErrors
().
get
(
0
).
getDefaultMessage
()),
httpStatus
);

}

if
(
e
instanceof

MethodArgumentTypeMismatchException
){

MethodArgumentTypeMismatchException
methodArgumentTypeMismatchException
=(
MethodArgumentTypeMismatchException
)
e
;
logger
.
error
(
“参数转换失败,方法:”
+
methodArgumentTypeMismatchException
.
getParameter
().
getMethod
().
getName
()+
“,参数:”
+
methodArgumentTypeMismatchException
.
getName
()+
“,信息:”
+
methodArgumentTypeMismatchException
.
getLocalizedMessage
());

return

new

ResponseEntity
<>(
new

ExceptionEntity
(
httpStatus
.
value
(),
“参数转换失败”
),
httpStatus
);

}

return

new

ResponseEntity
<>(
new

ExceptionEntity
(
httpStatus
.
value
(),
“参数转换失败”
),
httpStatus
);

}
}
创建CustomException 继承 RuntimeException类

代码示例

public

class

CustomException
extends
RuntimeException
{

private

static
final
long
serialVersionUID

8400239090334588012L
;

private

int
code
;

public

CustomException
(){
super
();

}

public

CustomException
(
int
code
,
String
message
){
super
(
message
);

this
.
setCode
(
code
);

}

public

int
getCode
()

{

return
code
;

}

public

void
setCode
(
int
code
)

{

this
.
code

code
;

}
}
创建ErrorResponseEntity实体类,用存放异常信息

代码示例

public

class

CustomException
extends
RuntimeException
{

private

static
final
long
serialVersionUID

8400239090334588012L
;

private

int
code
;

public

CustomException
(){
super
();

}

//忽略get,set
}
测试

代码示例

@RestController
public

class

TestController

{

@GetMapping
(
“/test”
)

public

String
test
(
Integer
number
){

if

(
null

==
number
){

throw

new

CustomException
(
110
,
“number不能为空,其实是400”
);

}

int
i

100
/
number
;

return

“你输入的数字缩小100倍是:”
+
i
;

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值