网站建设市区,苏州网站开发公司排名,免费素材大全视频,网站备案号查询拦截异常
在Spring Boot中#xff0c;我们可以将异常统一放在全局处理类来处理#xff0c;创建一个全局异常处理类需要用到ControllerAdvice和ExceptionHandler注解。
ControllerAdvice类似一个增强版的Controller#xff0c;用于标注类#xff0c;表示该类声明了整个项目…拦截异常
在Spring Boot中我们可以将异常统一放在全局处理类来处理创建一个全局异常处理类需要用到ControllerAdvice和ExceptionHandler注解。
ControllerAdvice类似一个增强版的Controller用于标注类表示该类声明了整个项目的全局资源
ExceptionHandler类似catch语句标注一个方法用于处理异常。而这个方法可以有返回值类似Controller的返回值
ExceptionHandler可以传入一个参数为异常类的Class对象 接下来举个例子
创建一个exception包在这个包下面写入一个异常处理类
package com.example.c0101.exceptionimport org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBodyControllerAdvice
class GlobalExceptionHandle {ExceptionHandler(NullPointerException::class)ResponseBodyfun handler(): String{return 参数不能为空}}
这表示当遇到空指针异常时向前端发送消息“参数不能为空”
接下来创建一个controller包。在这个包下面写一个控制器类
package com.example.c0101.controllerimport org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestControllerRestController
class TestController {RequestMapping(/index)fun index(name: String): String{return 您的用户名是$name}}
在/index的URI下用户需要传入一个name参数如果用户没有传入则会触发空指针异常调用我们之前写的异常处理类
我们使用postman测试一下 可以看到如果我们不传入name参数则会调用异常处理类并返回“参数不能为空” 获取异常日志
我们可以使用slf4j打印异常日志
log.error(参数为空, e)
这里面log.error可以接受两个参数第一个是提示字符串第二个是异常对象 修改异常处理类如下
package com.example.c0101.exceptionimport org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBodyControllerAdvice
class GlobalExceptionHandle {companion object{val log LoggerFactory.getLogger(GlobalExceptionHandle::class.java)}ExceptionHandler(NullPointerException::class)ResponseBodyfun handler(e: NullPointerException): String{log.error(参数为空, e)return 参数不能为空}}
这样我们就可以在出现异常时打印异常日志了。用postman不传入参数发现控制台输出 说明我们成功的在日志中输出了错误信息
修改异常状态下的响应码
我们可以通过ResponseStatus注解设置响应码如
ResponseStatus(HttpStatus.BAD_REQUEST) 修改异常处理类使其能够在出现空指针异常时返回400Bad Request状态码
package com.example.c0101.exceptionimport org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.ResponseStatusControllerAdvice
class GlobalExceptionHandle {companion object{val log LoggerFactory.getLogger(GlobalExceptionHandle::class.java)}ExceptionHandler(NullPointerException::class)ResponseBodyResponseStatus(HttpStatus.BAD_REQUEST)fun handler(e: NullPointerException): String{log.error(参数为空, e)return 参数不能为空}}
用postman不加参数测试一下 可以看到服务器最终返回了400状态码
只拦截某个包下发生的异常
我们可以通过ControllerAdvice的默认属性或basePackages属性只拦截某个包下发生的异常
ControllerAdvice(com.example.c0101.controller)
ControllerAdvice(basePackages [com.example.c0101.controller])
只拦截被某个注解标注的类发生的异常
我们可以通过ControllerAdvice的annotations属性只拦截某个注解标注的类发生的异常
ControllerAdvice(annotations [RestController::class])
拦截自定义异常
我们可以在exception包下自定义异常并使用拦截普通异常的方法拦截自定义异常
package com.example.c0101.exceptionclass MyException(msg: String) : Exception(msg)
在异常处理类里设置要拦截的异常
ExceptionHandler(MyException::class)
ResponseBody
fun handler(e: MyException): String{log.error(自定义异常, e)return 出现自定义异常
}
这样就可以拦截自定义异常了