异常捕获

```java     

package com.imooc.sell.handler;

import com.imooc.sell.config.ProjectUrl;
import com.imooc.sell.exception.SellerAuthorizeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class SellerAuthorizeExceptionHandler {

    @Autowired
    private ProjectUrl projectUrl;

    @ExceptionHandler(SellerAuthorizeException.class)
    public ModelAndView handler(){


        return new ModelAndView("redirect:"
                .concat(projectUrl.getWechatOpenAuthorize())
                .concat("/sell/seller/qrAuthorize?returnUrl=")
                .concat(projectUrl.getSell())
                .concat("/sell/seller/login"));

    }

}




记录一下使用的异常捕获 

    ```java      
    @ControllerAdvice

注解 配合 @ExceptionHandler(异常类class)

可以统一处理某一种或者多种异常
在某些特定场景 配合自定义异常使用
非常好用


补充注解

 @ResponseStatus()

该注解可以控制捕获异常后返回的http状态码点进注解可以看到枚举类应对特定需求很好用


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。邮件至 wcfinyourheart@163.com

文章标题:异常捕获

本文作者:wcf

发布时间:2020-01-17, 17:32:02

最后更新:2020-03-02, 10:06:10

原始链接:http://1007638786.github.io/2020/01/17/%E5%BC%82%E5%B8%B8%E6%8D%95%E8%8E%B7/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏