一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - springboot springmvc拋出全局異常的解決方法

springboot springmvc拋出全局異常的解決方法

2020-11-25 10:46全力以赴001 Java教程

這篇文章主要為大家詳細介紹了springboot springmvc拋出全局異常的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

1.定義異常捕獲

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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.定義一個返回對象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.最后一個測試類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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這個文件的配置

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚久久伊人精品青青草原2020 | 80日本xxxxxxxxx96 7个黑人玩北条麻妃 | 色聚网久久综合 | 国产精品久久久久久影院 | 99久久精品免费看国产情侣 | 妇女澡堂淋浴性 | 免费一级特黄特色大片∵黄 | 欧美a在线 | 7788理论片在线观看 | 四虎精品视频在线永久免费观看 | 免费高清视频日本 | 亚洲成在人线视频 | 边吃奶边扎下面 | 免费看视频的 | 五月一区二区久久综合天堂 | 日韩成人在线免费视频 | 亚洲性69影视 | 挺进白嫩老师下面视频 | 欧美人曾交| 亚洲国产日韩欧美在线vip1区 | 天天操天天射天天爽 | 久久亚洲精品AV无码四区 | 舔大逼| 好大好硬好长好爽a网站 | 美女扒开奶罩让男人吃奶 | 精品视频一区二区 | 日韩亚洲欧美综合一区二区三区 | 亚洲 欧美 中文 日韩 另类 | 大肚孕妇的高h辣文 | 桃乃木香奈作品在线观看 | china国产bbw| 四虎色影院 | 波多野结衣在线观看中文字幕 | 扒开双腿羞辱调教play视频 | 国产午夜免费 | 亚洲 日韩 在线 国产 视频 | 青青热久麻豆精品视频在线观看 | 男人躁女人过程 | 亚洲精品国产在线 | 精品国产区一区二区三区在线观看 | 美女扒开胸罩露出奶了无遮挡免费 |