溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot錯(cuò)誤處理機(jī)制以及自定義異常處理詳解

發(fā)布時(shí)間:2020-09-01 10:48:32 來(lái)源:腳本之家 閱讀:191 作者:不清不慎 欄目:編程語(yǔ)言

上篇文章我們講解了使用Hibernate Validation來(lái)校驗(yàn)數(shù)據(jù),當(dāng)校驗(yàn)完數(shù)據(jù)后,如果發(fā)生錯(cuò)誤我們需要給客戶返回一個(gè)錯(cuò)誤信息,因此這節(jié)我們來(lái)講解一下SpringBoot默認(rèn)的錯(cuò)誤處理機(jī)制以及如何自定義異常來(lái)處理請(qǐng)求錯(cuò)誤。

一、SpringBoot默認(rèn)的錯(cuò)誤處理機(jī)制

我們?cè)诎l(fā)送一個(gè)請(qǐng)求的時(shí)候,如果發(fā)生404 SpringBoot會(huì)怎么處理呢?我們來(lái)發(fā)送一個(gè)不存在的請(qǐng)求來(lái)驗(yàn)證一下看看頁(yè)面結(jié)果。如下所示:

SpringBoot錯(cuò)誤處理機(jī)制以及自定義異常處理詳解

當(dāng)服務(wù)器內(nèi)部發(fā)生錯(cuò)誤的時(shí)候,頁(yè)面會(huì)返回什么呢?

 @GetMapping("/user/{id:\\d+}")
 public User get(@PathVariable String id) {
  throw new RuntimeException();
 }

SpringBoot錯(cuò)誤處理機(jī)制以及自定義異常處理詳解

我們會(huì)發(fā)現(xiàn)無(wú)論是發(fā)生什么錯(cuò)誤,SpringBoot都會(huì)返回一個(gè)狀態(tài)碼以及一個(gè)錯(cuò)誤頁(yè)面,這個(gè)錯(cuò)誤頁(yè)面是怎么來(lái)的呢?
我們來(lái)看看SpringBoot錯(cuò)誤處理模塊的源碼就會(huì)非常清楚,默認(rèn)的發(fā)生錯(cuò)誤,它會(huì)將請(qǐng)求轉(zhuǎn)發(fā)到BasicErrorController控制器來(lái)處理請(qǐng)求,下面是該controller類的源碼:

@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {

 private final ErrorProperties errorProperties;

 /**
  * Create a new {@link BasicErrorController} instance.
  * @param errorAttributes the error attributes
  * @param errorProperties configuration properties
  */
 public BasicErrorController(ErrorAttributes errorAttributes,
   ErrorProperties errorProperties) {
  this(errorAttributes, errorProperties,
    Collections.<ErrorViewResolver>emptyList());
 }

 /**
  * Create a new {@link BasicErrorController} instance.
  * @param errorAttributes the error attributes
  * @param errorProperties configuration properties
  * @param errorViewResolvers error view resolvers
  */
 public BasicErrorController(ErrorAttributes errorAttributes,
   ErrorProperties errorProperties, List<ErrorViewResolver> errorViewResolvers) {
  super(errorAttributes, errorViewResolvers);
  Assert.notNull(errorProperties, "ErrorProperties must not be null");
  this.errorProperties = errorProperties;
 }

 @Override
 public String getErrorPath() {
  return this.errorProperties.getPath();
 }

 @RequestMapping(produces = "text/html")
 public ModelAndView errorHtml(HttpServletRequest request,
   HttpServletResponse response) {
  HttpStatus status = getStatus(request);
  Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
    request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
  response.setStatus(status.value());
  ModelAndView modelAndView = resolveErrorView(request, response, status, model);
  return (modelAndView == null ? new ModelAndView("error", model) : modelAndView);
 }

 @RequestMapping
 @ResponseBody
 public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
  Map<String, Object> body = getErrorAttributes(request,
    isIncludeStackTrace(request, MediaType.ALL));
  HttpStatus status = getStatus(request);
  return new ResponseEntity<Map<String, Object>>(body, status);
 }

從上面的源碼我們可以看到,它有兩個(gè)RequestMapping方法來(lái)映射錯(cuò)誤請(qǐng)求,為什么會(huì)是兩個(gè)呢?其實(shí)errorHtml方法映射的是瀏覽器發(fā)送來(lái)的請(qǐng)求,而error方法映射的是不是瀏覽器而是其他軟件app客戶端發(fā)送的錯(cuò)誤請(qǐng)求。

看了上面的源碼后,我們是否可以自己定義404或者500的錯(cuò)誤頁(yè)面返回給客戶端呢?當(dāng)然可以,我們可以在src/main/resources路徑下新建文件夾reources/error文件夾,然后新建404.html和500.html然后編寫自己的錯(cuò)誤內(nèi)容即可:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>404</title>
</head>
<body>
 親,您所訪問(wèn)的頁(yè)面不存在
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>500</title>
</head>
<body>
 服務(wù)器內(nèi)部錯(cuò)誤
</body>
</html>

不過(guò)注意的是上面的這種自定義頁(yè)面的方式只在瀏覽器端有效,而不是瀏覽器發(fā)送的請(qǐng)求不會(huì)生效。因此下面我們就講一下如何自定義異常處理來(lái)解決這個(gè)問(wèn)題。

二、自定義異常處理

怎么自定義異常處理客戶端發(fā)送的錯(cuò)誤信息呢?如果我們查詢一個(gè)用戶,該用戶不存在,我們是否可以將不存在的用戶的id返回給客戶呢?這樣的效果不是給客戶更好地體驗(yàn)嗎?下面我們來(lái)實(shí)現(xiàn)這個(gè)功能。
首先我們需要編寫一個(gè)exception類繼承RuntimeException類:

package cn.shinelon.exception;

/**
 * @author Shinelon
 *
 */
public class UserNotExistException extends RuntimeException{

 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 private String id;
 public UserNotExistException(String id) {
  super("user not exist");
  this.id=id;
 }
public void setId(String id) {
 this.id = id;
}
public String getId() {
 return id;
}
}

接著我們需要編寫一個(gè)handler類處理controller層拋出的異常:

/**
 * 
 */
package cn.shinelon.exception;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 控制器的異常處理類
 * @author Shinelon
 *
 */
//這個(gè)注解是指這個(gè)類是處理其他controller拋出的異常
@ControllerAdvice
public class ControllerExceptionHandler {

 //這個(gè)注解是指當(dāng)controller中拋出這個(gè)指定的異常類的時(shí)候,都會(huì)轉(zhuǎn)到這個(gè)方法中來(lái)處理異常
 @ExceptionHandler(UserNotExistException.class)
 //將返回的值轉(zhuǎn)成json格式的數(shù)據(jù)
 @ResponseBody
 //返回的狀態(tài)碼
 @ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)  //服務(wù)內(nèi)部錯(cuò)誤
 public Map<String,Object> handlerUserNotExistException(UserNotExistException ex){
  Map<String,Object> result=new HashMap<String,Object>();
  result.put("id", ex.getId());
  result.put("message", ex.getMessage());
  return result;
 }
}

這個(gè)類加上@ControllerAdvice注解將會(huì)處理controller層拋出的對(duì)應(yīng)的異常,這里我們處理controller拋出的UserNotExistException自定義異常,并且將錯(cuò)誤信息以及用戶id以json串的格式返回給客戶。

接著,我們?cè)赾ontroller的請(qǐng)求方法中拋出這個(gè)異常,會(huì)看到在瀏覽器中的異常是我們自定義的異常返回的json數(shù)據(jù)。

Controller層代碼:

@GetMapping("/user/{id:\\d+}")
 //@RequestMapping(value="/user/{id:\\d+}",method=RequestMethod.GET)
 @JsonView(User.DetailJsonView.class)
 public User get(@PathVariable String id) {
  throw new UserNotExistException(id);
 }

SpringBoot錯(cuò)誤處理機(jī)制以及自定義異常處理詳解

到這里,我們就介紹了SpringBoot默認(rèn)的錯(cuò)誤處理機(jī)制以及我們自定義異常來(lái)處理錯(cuò)誤請(qǐng)求,這更有利于我們的開(kāi)發(fā),帶給用戶更佳的使用效果。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI