springboot异常与重定向如何实现

发布时间:2021-12-20 08:51:31 作者:iii
来源:亿速云 阅读:237

本篇内容主要讲解“springboot异常与重定向如何实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springboot异常与重定向如何实现”吧!

springboot 异常与重定向

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

默认调用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;
    }

结果:

springboot异常与重定向如何实现

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

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

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

结果:

springboot异常与重定向如何实现

这种方式重定向,都是通过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");
    }

结果:

springboot异常与重定向如何实现

无论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!");
    }

结果:

springboot异常与重定向如何实现

springboot异常与重定向如何实现

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

5.自己定义异常

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

结果:

springboot异常与重定向如何实现

springboot异常与重定向如何实现

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

springboot 异常统一处理

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

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

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

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

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

流程 :

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);
    }
 }

流程:

到此,相信大家对“springboot异常与重定向如何实现”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. 如何实现SpringBoot中的异常处理与参数校验
  2. Springboot转发重定向实现方式解析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot

上一篇:如何使用Pygame模块实现大战外星人游戏

下一篇:javascript七大数据类型有哪些呢

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》