龙哥网

龙哥网

springboot 实战:异常与重定向问题_java
2022-03-01

目录
  • springboot 异常与重定向
    • 1.下面先通过一个简单的例子实现页面的重定向
    • 2.通过一个更简单的方法实现重定向
    • 3.在重定向过程中,用session传递信息
    • 4.admin请求异常
    • 5.自己定义异常
  • springboot 异常统一处理
    • Demo

springboot 异常与重定向

在spring中,有两个重定向类型:

  • 301,永久性跳转
  • 302,暂时性跳转

默认调用302。

1.下面先通过一个简单的例子实现页面的重定向

@RequestMapping("/redirect/[code]")
    public RedirectView redirectView(@PathVariable("code") int code,
                               HttpSession session){
 
        RedirectView red = new RedirectView("/",true);
            //判断是不是301异常
            if (code == 301){
                //默认为302转移,此处设置为永久性转移
                red.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
            }
            return red;
    }

结果:

无论是访问“redirect/301”还是“redirect/302”,结果都会跳转到首页,只是一个是301类型,一个是302类型。

2.通过一个更简单的方法实现重定向

    @RequestMapping("/redirect/[code]")
    public RedirectView redirectView(@PathVariable("code") int code,
                               HttpSession session){
 
        //这种跳转都是302跳转,通过一个redirect前缀告诉请求,要跳转到首页
        //所有的redirect请求都会跳转到首页
        return "redirect:/";
    }

结果:

这种方式重定向,都是通过302的方式进行的,无论“redirect”后面的url是什么,因为只要识别到redirect这个前缀,就会跳转到首页。

3.在重定向过程中,用session传递信息

1.重定向页面
    @RequestMapping("/redirect/[code]")
    public String redirectView(@PathVariable("code") int code,
                               HttpSession session){
 
        //这种跳转都是302跳转,通过一个redirect前缀告诉请求,要跳转到首页
        //所有的redirect请求都会跳转到首页
        //通过session来传递信息
        session.setAttribute("msg","Jump from redirect");
        return "redirect:/";
    }
2.首页
    @RequestMapping("/")
    @ResponseBody
    public String index(HttpSession session){
        //在首页中显示重定向中的session
        return "Hello World!" + session.getAttribute("msg");
    }

结果:

无论redirect后面的url是什么,都会返回首页,并显示相应的信息。

4.admin请求异常

    @RequestMapping("/admin")
    @ResponseBody
    public String admin(@RequestParam("key") String key){
    //如果key=“admin”
        if ("admin".equals(key)){
            return "hello admin";
        }
    //否则抛出异常
        throw new IllegalArgumentException("Key Wrong!");
    }

结果:

在“key=admin”的时候,返回相应信息;在“key!=admin”的时候,返回错误信息。

5.自己定义异常

   @ExceptionHandler()
    @ResponseBody
    public String error(Exception e){
        return "error:" + e.getMessage();
    }

结果:

可以看出,在出现异常的时候,使我们自己定义的异常界面内容,和4中的不同。

springboot 异常统一处理

这里先对需要使用到的注解或者类进行说明,顺便理清楚条理。

@ExceptionHandler:注解使用在方法上,值为指定某个异常,当该方法所在的controller出现的异常与注解的异常对应,则会触发注解的方法。

下面这个controller一旦出现异常都会将异常捕获转给该方法进行处理

@RestController
@RequestMapping("user")
public class UserController {
    @ExceptionHandler(value = Exception.class)
    public void solveException(){
        //异常处理逻辑
    }
    
}

@controllerAdvice: 注解在类上,注解的类会注册到spring容器中,类中可有三种注解,@ExceptionHandler,@InitBinder,@ModelAttribute。该类下只要是注解上面三个注解的方法就是让把方法应用到程序中所有带有@RequesMapping注解的方法上。

流程 :

  • 自定义一个自己的异常
  • 声明带有@ControllerAdvice的类和@ExceptionHandler的方法,将@ExceptionHandler的方法应用到所有controller。
  • 声明一个返回结果类
  • 声明一个枚举类,用来包含可能出现的异常类型

Demo

自定义异常:

@Data
@AllArgsConstructor
public class MyException extends RuntimeException{
    private Integer code;
    private String msg;
    public MyException(ResultEnum resultEnum){
        this.msg = resultEnum.getMsg();
        this.code = resultEnum.getCode();
    }
}

自定义返回结果:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
    private int code;
    private String msg;
}

枚举类:

public enum  ResultEnum {
    UNKNOW_ERROR(-1,"未知错误"),
    USER_ERROR(-2,"用户信息错误"),
    SUCCESS(0,"成功");
    private  Integer code;
    private  String msg;
    ResultEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    //省略getter和setter
}

工具类:

public class ResultUtil {
    public static Result error(Integer code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
}

自定义异常捕获类:

@ControllerAdvice
@Slf4j
public class MyExceptionHandler {
 //接收的是Exception,也就是只要是异常都会执行这方法
    @ExceptionHandler(value=Exception.class)
    @ResponseBody
    public Result handle(Exception e){
        if(e instanceof MyException){
            MyException myException = (MyException) e;
            return ResultUtil.error(myException.getCode(),myException.getMsg());
        }else{
            return ResultUtil.error(-1,"未知错误");
        }
    }
}

controller类:

@RestController
@RequestMapping("user")
public class UserController {
    @GetMapping("exception")
    public void catchException() throws Exception{
        throw new MyException(ResultEnum.USER_ERROR);
    }
 }

流程:

  • 我们访问http://localhost:8080/user/exception来访问该方法,并抛出我们自定义的异常,通过枚举类来进行对异常信息的集合。
  • 通过自定义的异常捕获类,来进行对异常的捕获,执行方法。
  • 异常捕获类的方法中,通过ResultUtil工具类来进行生成Result对象返回。
免责声明
本站部分资源来源于互联网 如有侵权 请联系站长删除
龙哥网是优质的互联网科技创业资源_行业项目分享_网络知识引流变现方法的平台为广大网友提供学习互联网相关知识_内容变现的方法。