springboot 如何优雅的抛出异常

15 篇文章 0 订阅
10 篇文章 0 订阅

之前项目的异常是一层一层抛出去及其麻烦,并且每个层都要try catch 然后把错误信息包装返回到最顶层 ,从service一层一层抛出去。人都写傻了,后面发现了springboot项目有全局拦截器。还是看的太浅了,先随便创建一个

全局拦截MyControllerAdvice.java

/**
 * controller 增强器
 */
@ControllerAdvice
public class MyControllerAdvice {
    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = RrException.class)
    public Result errorHandler(RrException ex) {
        return Result.error(ex.getMessage());
    }

}

其中的RrException 是我自定义的异常 因为我自己要抛出  但是Exception又需要捕捉  ,而自定义的错误就不用

异常类RrException .class

/**
 * 自定义异常
 * @author zyc
 */
public class RrException extends RuntimeException {
	private static final long serialVersionUID = 1L;
	
    private String msg;
    private int code = 500;
    
    public RrException(String msg) {
		super(msg);
		this.msg = msg;
	}
	
	public RrException(String msg, Throwable e) {
		super(msg, e);
		this.msg = msg;
	}
	
	public RrException(String msg, int code) {
		super(msg);
		this.msg = msg;
		this.code = code;
	}
	
	public RrException(String msg, int code, Throwable e) {
		super(msg, e);
		this.msg = msg;
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}
	
	
}

这样在

这样后端报的错误就会被拦截然后返回一个Result对象到前端

结果类Result.class

public class Result extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;
	
	public Result() {
		put("code", 0);
		put("msg", "success");
	}
	
	public static Result error() {
		return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常,请联系管理员");
	}
	
	public static Result error(String msg) {
		return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
	}
	
	public static Result error(int code, String msg) {
		Result r = new Result();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	public static Result ok(String msg) {
		Result r = new Result();
		r.put("msg", msg);
		return r;
	}
	
	public static Result ok(Map<String, Object> map) {
		Result r = new Result();
		r.putAll(map);
		return r;
	}
	
	public static Result ok() {
		return new Result();
	}

	@Override
	public Result put(String key, Object value) {
		super.put(key, value);
		return this;
	}
}

参考自https://www.jianshu.com/p/383725700b7b

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 中,可以使用 `SpringApplication` 类的 `WebApplicationType` 属性来控制应用的 Web 环境,从而实现优雅停止接收 HTTP 请求的功能。具体实现方式如下: 1. 在启动类中,设置 `WebApplicationType` 属性为 `REACTIVE` 或 `SERVLET`。`REACTIVE` 表示使用 Spring WebFlux,`SERVLET` 表示使用 Spring MVC。注意,如果你的应用中同时存在 WebFlux 和 MVC,那么需要使用 `NONE`,否则会抛出异常。 ```java @SpringBootApplication public class MyApp { public static void main(String[] args) { SpringApplication app = new SpringApplication(MyApp.class); app.setWebApplicationType(WebApplicationType.REACTIVE); // 或 SERVLET app.run(args); } } ``` 2. 在 `application.properties` 或 `application.yaml` 文件中,设置 `server.shutdown` 属性为 `graceful`,表示使用优雅停止的方式关闭应用。 ```yaml server: shutdown: graceful ``` 3. 在 Spring Boot 应用中添加一个 ShutdownHook,当应用被关闭时,会调用这个 ShutdownHook。 ```java @Component public class GracefulShutdown implements ApplicationListener<ContextClosedEvent> { private final Logger logger = LoggerFactory.getLogger(getClass()); private volatile boolean isShuttingDown = false; @Override public void onApplicationEvent(ContextClosedEvent event) { if (!isShuttingDown) { isShuttingDown = true; logger.info("Shutting down gracefully..."); try { Thread.sleep(5000); // 模拟执行任务的时间 } catch (InterruptedException e) { // 异常处理 } } } } ``` 在上面的代码中,我们使用了一个 `volatile` 变量 `isShuttingDown` 来记录应用是否正在关闭。当应用被关闭时,会调用 `onApplicationEvent` 方法,这里我们模拟执行任务的时间为 5 秒。注意,如果你使用的是 WebFlux,那么需要使用 `GracefulShutdownHandler` 类来实现优雅关闭,具体使用方式可以参考官方文档。 总的来说,使用 Spring Boot 实现优雅停止接收 HTTP 请求的方式比较简单,只需要设置 WebApplicationType 和 server.shutdown 属性即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值