溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在springboot中springmvc出現拋出全局異常如何解決

發布時間:2020-11-25 15:43:45 來源:億速云 閱讀:273 作者:Leah 欄目:編程語言

在springboot中springmvc出現拋出全局異常如何解決?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

springboot中拋出異常,springboot自帶的是springmvc框架,這個就不多說了。

springmvc統一異常解決方法這里要說明的是。只是結合了springboot的使用而已。直接上代碼,有效有用的才是ok。

1.定義異常捕獲

package com.example.rest.error;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolationException;

/**
 *
 * @author ming 定義全局異常處理
 * @RestControllerAdvice 是@controlleradvice 與@ResponseBody 的組合注解
 */
@RestControllerAdvice 
public class GlobalControllerExceptionHandler {

 @ExceptionHandler(value = { ConstraintViolationException.class })
 @ResponseStatus(HttpStatus.BAD_REQUEST)
 public ApiErrorResponse constraintViolationException(ConstraintViolationException ex) {
  return new ApiErrorResponse(500, 5001, ex.getMessage());
 }
 
 @ExceptionHandler(value = { IllegalArgumentException.class })
 @ResponseStatus(HttpStatus.BAD_REQUEST)
 public ApiErrorResponse IllegalArgumentException(IllegalArgumentException ex) {
  return new ApiErrorResponse(501, 5002, ex.getMessage());
 }

 @ExceptionHandler(value = { NoHandlerFoundException.class })
 @ResponseStatus(HttpStatus.NOT_FOUND)
 public ApiErrorResponse noHandlerFoundException(Exception ex) {
  return new ApiErrorResponse(404, 4041, ex.getMessage());
 }

 
 @ExceptionHandler(value = { Exception.class })
 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
 public ApiErrorResponse unknownException(Exception ex) {
  return new ApiErrorResponse(500, 5002, ex.getMessage());
 }
}

2.定義一個返回對象

package com.example.rest.error;

/**
 * @author ming
 */
public class ApiErrorResponse {

 private int status;
 private int code;
 private String message;

 public ApiErrorResponse(int status, int code, String message) {
  this.status = status;
  this.code = code;
  this.message = message;
 }

 public int getStatus() {
  return status;
 }

 public int getCode() {
  return code;
 }

 public String getMessage() {
  return message;
 }

 @Override
 public String toString() {
  return "ApiErrorResponse{" +
    "status=" + status +
    ", code=" + code +
    ", message=" + message +
    '}';
 }
}

3.定義一個啟動Application

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@SpringBootApplication
@EnableWebMvc 
public class SpringBootExceptionHandlingApplication {

 public static void main(String[] args) {
  SpringApplication.run(SpringBootExceptionHandlingApplication.class, args);
 }
}

4.最后一個測試類

package com.example.rest.controller;

import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.ConstraintViolationException;
import java.util.Collections;

/**
 * @author ming
 */
@RestController
public class TestController {

 @GetMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
 public void test(Long id) {
  Assert.notNull(id,"id不能為空!");
  throw new ConstraintViolationException("error", Collections.emptySet());
 }
}

注意application.properties這個文件的配置

spring.mvc.throw-exception-if-no-handler-found=true 

ok,springboot中解決springmvc異常拋出就可以這樣解決了。

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女