本篇内容主要讲解“springboot异常与重定向如何实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springboot异常与重定向如何实现”吧!
在spring中,有两个重定向类型:
301,永久性跳转
302,暂时性跳转
默认调用302。
@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类型。
@RequestMapping("/redirect/[code]")
public RedirectView redirectView(@PathVariable("code") int code,
HttpSession session){
//这种跳转都是302跳转,通过一个redirect前缀告诉请求,要跳转到首页
//所有的redirect请求都会跳转到首页
return "redirect:/";
}
结果:
这种方式重定向,都是通过302的方式进行的,无论“redirect”后面的url是什么,因为只要识别到redirect这个前缀,就会跳转到首页。
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是什么,都会返回首页,并显示相应的信息。
@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”的时候,返回错误信息。
@ExceptionHandler()
@ResponseBody
public String error(Exception e){
return "error:" + e.getMessage();
}
结果:
可以看出,在出现异常的时候,使我们自己定义的异常界面内容,和4中的不同。
这里先对需要使用到的注解或者类进行说明,顺便理清楚条理。
@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。
声明一个返回结果类
声明一个枚举类,用来包含可能出现的异常类型
自定义异常:
@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对象返回。
到此,相信大家对“springboot异常与重定向如何实现”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。