springboot springmvc拋出全局異常的解決方法
springboot中拋出異常,springboot自帶的是springmvc框架,這個就不多說了。
springmvc統(tǒng)一異常解決方法這里要說明的是。只是結(jié)合了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異常拋出就可以這樣解決了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
spring boot之SpringApplication 事件監(jiān)聽
這篇文章主要介紹了spring boot之SpringApplication 事件監(jiān)聽,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
下面小編就為大家?guī)硪黄狫ava創(chuàng)建數(shù)組的幾種方式總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能給大家?guī)韼椭?/div> 2021-06-06
springboot 啟動時初始化數(shù)據(jù)庫的步驟
這篇文章主要介紹了springboot 啟動時初始化數(shù)據(jù)庫的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2021-01-01
Java 線程池ThreadPoolExecutor源碼解析
這篇文章主要介紹了Java 線程池ThreadPoolExecutor源碼解析2022-03-03
Mybatis Criteria使用and和or進行聯(lián)合條件查詢的操作方法
這篇文章主要介紹了Mybatis Criteria的and和or進行聯(lián)合條件查詢的方法,本文通過例子給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10
基于jdk動態(tài)代理和cglib動態(tài)代理實現(xiàn)及區(qū)別說明
這篇文章主要介紹了基于jdk動態(tài)代理和cglib動態(tài)代理實現(xiàn)及區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
java中對象和Map互相轉(zhuǎn)換的幾種常見方式舉例
Map在日常開發(fā)應用中的頻率很高,最常用的實現(xiàn)類是HashMap和有序的TreeMap,下面這篇文章主要給大家介紹了關于java中對象和Map互相轉(zhuǎn)換的幾種常見方式舉例,需要的朋友可以參考下2024-01-01最新評論

