springcloud springboot 异常拦截器 项目完善03
> 创建异常拦截器,在controller层没处理的异常抛出时,异常拦截器拦截后输入自定义的报错信息。
新建GlobalExceptionAdvice.java
GlobalExceptionAdvice.java 在com.base.web目录 需要配置包扫描路径
@ComponentScan(basePackages ={"com.base","com.wying"})
WrapperRusult文件在
springcloud springboot 统一返回结果对象WrapperRusult 项目完善02
中
package com.base.web;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
/**
* description:RESTful api 全局异常处理
* date: 2022/4/18
* author: gaom
* version: 1.0
*/
@RestControllerAdvice
public class GlobalExceptionAdvice {
/**
* 开启调试模式
*/
public static boolean DEV_MODE=true;
/**
* 拦截controller层没处理的异常
* @param httpServletRequest
* @param exception
* @return
*/
@ExceptionHandler(Exception.class)
public WrapperRusult defaultErrorHandler(HttpServletRequest httpServletRequest ,Exception exception){
exception.printStackTrace();
return new WrapperRusult(WrapperRusult.ResultEnum.enum_fail.getCode(), WrapperRusult.ResultEnum.enum_fail.getType(), DEV_MODE?getErrorInfoFromException(exception):WrapperRusult.ResultEnum.enum_fail.getMessage(),new HashMap<>());
}
/**
* 获取string格式异常信息
* @param exception
* @return
*/
public static String getErrorInfoFromException(Exception exception) {
StringWriter sw=null;
PrintWriter pw=null;
try {
sw = new StringWriter();
pw = new PrintWriter(sw);
exception.printStackTrace(pw);
return sw.toString();
} catch (Exception exception2) {
return exception2.getMessage();
}finally {
try {
sw.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
pw.close();
}
}
}
controller层增加测试代码
package com.wying.myspringbootframework.controller;
import com.base.web.WrapperRusult;
import com.wying.myspringbootframework.entity.YyptTestEntity;
import com.wying.myspringbootframework.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* description:测试请求
* date: 2022/3/31
* author: gaom
* version: 1.0
*/
@RestController
@RequestMapping("/testController")
public class TestController {
@Autowired
public TestService testService;
@GetMapping("/findAll")
public List<YyptTestEntity> findAll(){
List<YyptTestEntity> list=testService.findAll();
return list;
}
@GetMapping(value = "/findAll_testWrapperRusult")
public WrapperRusult findAll_testWrapperRusult(){
List<YyptTestEntity> list=testService.findAll();
return WrapperRusult.success(list);
}
@GetMapping(value = "/findAll_testGlobalExceptionAdvice")
public WrapperRusult findAll_testGlobalExceptionAdvice(){
List<YyptTestEntity> list=testService.findAll();
Integer.parseInt("a");
return WrapperRusult.success(list);
}
}
调用测试
http://127.0.0.1:7015/myspringbootframework/testController/findAll_testGlobalExceptionAdvice
代码中制造了一个Integer.parseInt(“a”);转换异常