springboot?實戰(zhàn):異常與重定向問題
springboot 異常與重定向
在spring中,有兩個重定向類型:
- 301,永久性跳轉(zhuǎn)
- 302,暫時性跳轉(zhuǎn)
默認(rèn)調(diào)用302。
1.下面先通過一個簡單的例子實現(xiàn)頁面的重定向
@RequestMapping("/redirect/[code]")
public RedirectView redirectView(@PathVariable("code") int code,
HttpSession session){
RedirectView red = new RedirectView("/",true);
//判斷是不是301異常
if (code == 301){
//默認(rèn)為302轉(zhuǎn)移,此處設(shè)置為永久性轉(zhuǎn)移
red.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
}
return red;
}
結(jié)果:

無論是訪問“redirect/301”還是“redirect/302”,結(jié)果都會跳轉(zhuǎn)到首頁,只是一個是301類型,一個是302類型。
2.通過一個更簡單的方法實現(xiàn)重定向
@RequestMapping("/redirect/[code]")
public RedirectView redirectView(@PathVariable("code") int code,
HttpSession session){
//這種跳轉(zhuǎn)都是302跳轉(zhuǎn),通過一個redirect前綴告訴請求,要跳轉(zhuǎn)到首頁
//所有的redirect請求都會跳轉(zhuǎn)到首頁
return "redirect:/";
}
結(jié)果:

這種方式重定向,都是通過302的方式進(jìn)行的,無論“redirect”后面的url是什么,因為只要識別到redirect這個前綴,就會跳轉(zhuǎn)到首頁。
3.在重定向過程中,用session傳遞信息
1.重定向頁面
@RequestMapping("/redirect/[code]")
public String redirectView(@PathVariable("code") int code,
HttpSession session){
//這種跳轉(zhuǎn)都是302跳轉(zhuǎn),通過一個redirect前綴告訴請求,要跳轉(zhuǎn)到首頁
//所有的redirect請求都會跳轉(zhuǎn)到首頁
//通過session來傳遞信息
session.setAttribute("msg","Jump from redirect");
return "redirect:/";
}
2.首頁
@RequestMapping("/")
@ResponseBody
public String index(HttpSession session){
//在首頁中顯示重定向中的session
return "Hello World!" + session.getAttribute("msg");
}
結(jié)果:

無論redirect后面的url是什么,都會返回首頁,并顯示相應(yīng)的信息。
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!");
}
結(jié)果:


在“key=admin”的時候,返回相應(yīng)信息;在“key!=admin”的時候,返回錯誤信息。
5.自己定義異常
@ExceptionHandler()
@ResponseBody
public String error(Exception e){
return "error:" + e.getMessage();
}
結(jié)果:


可以看出,在出現(xiàn)異常的時候,使我們自己定義的異常界面內(nèi)容,和4中的不同。
springboot 異常統(tǒng)一處理
這里先對需要使用到的注解或者類進(jìn)行說明,順便理清楚條理。
@ExceptionHandler:注解使用在方法上,值為指定某個異常,當(dāng)該方法所在的controller出現(xiàn)的異常與注解的異常對應(yīng),則會觸發(fā)注解的方法。
下面這個controller一旦出現(xiàn)異常都會將異常捕獲轉(zhuǎn)給該方法進(jìn)行處理
@RestController
@RequestMapping("user")
public class UserController {
@ExceptionHandler(value = Exception.class)
public void solveException(){
//異常處理邏輯
}
}
@controllerAdvice: 注解在類上,注解的類會注冊到spring容器中,類中可有三種注解,@ExceptionHandler,@InitBinder,@ModelAttribute。該類下只要是注解上面三個注解的方法就是讓把方法應(yīng)用到程序中所有帶有@RequesMapping注解的方法上。
流程 :
- 自定義一個自己的異常
- 聲明帶有@ControllerAdvice的類和@ExceptionHandler的方法,將@ExceptionHandler的方法應(yīng)用到所有controller。
- 聲明一個返回結(jié)果類
- 聲明一個枚舉類,用來包含可能出現(xiàn)的異常類型
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();
}
}
自定義返回結(jié)果:
@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,也就是只要是異常都會執(zhí)行這方法
@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來訪問該方法,并拋出我們自定義的異常,通過枚舉類來進(jìn)行對異常信息的集合。
- 通過自定義的異常捕獲類,來進(jìn)行對異常的捕獲,執(zhí)行方法。
- 異常捕獲類的方法中,通過ResultUtil工具類來進(jìn)行生成Result對象返回。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- springBoot熱部署、請求轉(zhuǎn)發(fā)與重定向步驟詳解
- springboot如何重定向外部網(wǎng)頁
- SpringBoot中處理的轉(zhuǎn)發(fā)與重定向方式
- 使用springboot跳轉(zhuǎn)到指定頁面和(重定向,請求轉(zhuǎn)發(fā)的實例)
- springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes
- springboot 重定向方式(redirect前綴)
- springboot項目攔截器重定向循環(huán)問題的解決
- 基于springboot redirect重定向路徑問題總結(jié)
- springboot 如何重定向redirect 并隱藏參數(shù)
- Springboot轉(zhuǎn)發(fā)重定向?qū)崿F(xiàn)方式解析
- SpringBoot后端服務(wù)重定向的實現(xiàn)示例
相關(guān)文章
SpringBoot項目導(dǎo)入外部jar包的詳細(xì)指南
在開發(fā)SpringBoot項目時,我們經(jīng)常需要引入一些外部的jar包來增強(qiáng)項目的功能,這些jar包可能不是Maven中央倉庫中的,或者我們想要使用特定版本的jar包,本文將詳細(xì)介紹如何在SpringBoot項目中導(dǎo)入外部jar包,需要的朋友可以參考下2024-10-10
JAVA實現(xiàn)單例模式的四種方法和一些特點(diǎn)
JAVA實現(xiàn)單例模式的四種方法和一些特點(diǎn),需要的朋友可以參考一下2013-03-03
java注解結(jié)合aspectj AOP進(jìn)行日志打印的操作
這篇文章主要介紹了java注解結(jié)合aspectj AOP進(jìn)行日志打印的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02

