springboot?實(shí)戰(zhàn):異常與重定向問題
springboot 異常與重定向
在spring中,有兩個(gè)重定向類型:
- 301,永久性跳轉(zhuǎn)
- 302,暫時(shí)性跳轉(zhuǎn)
默認(rèn)調(diào)用302。
1.下面先通過一個(gè)簡(jiǎn)單的例子實(shí)現(xiàn)頁(yè)面的重定向
@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é)果都會(huì)跳轉(zhuǎn)到首頁(yè),只是一個(gè)是301類型,一個(gè)是302類型。
2.通過一個(gè)更簡(jiǎn)單的方法實(shí)現(xiàn)重定向
@RequestMapping("/redirect/[code]")
public RedirectView redirectView(@PathVariable("code") int code,
HttpSession session){
//這種跳轉(zhuǎn)都是302跳轉(zhuǎn),通過一個(gè)redirect前綴告訴請(qǐng)求,要跳轉(zhuǎn)到首頁(yè)
//所有的redirect請(qǐng)求都會(huì)跳轉(zhuǎn)到首頁(yè)
return "redirect:/";
}
結(jié)果:

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

無論redirect后面的url是什么,都會(huì)返回首頁(yè),并顯示相應(yīng)的信息。
4.admin請(qǐng)求異常
@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”的時(shí)候,返回相應(yīng)信息;在“key!=admin”的時(shí)候,返回錯(cuò)誤信息。
5.自己定義異常
@ExceptionHandler()
@ResponseBody
public String error(Exception e){
return "error:" + e.getMessage();
}
結(jié)果:


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

