Springboot2.0自適應效果錯誤響應過程解析
這篇文章主要介紹了Springboot2.0自適應效果錯誤響應過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
實現(xiàn)效果當訪問thymeleaf渲染頁面時,顯示的是自定義的錯誤頁面
當以接口方式訪問時,顯示的是自定義的json數(shù)據(jù)響應
1. 編寫自定義異常
package cn.jfjb.crud.exception;
/**
* @author john
* @date 2019/11/24 - 9:48
*/
public class UserNotExistException extends RuntimeException {
public UserNotExistException() {
super("用戶不存在");
}
}
2. 自定義異常處理&返回定制json數(shù)據(jù),轉發(fā)到/error進行自適應響應效果處理
package cn.jfjb.crud.handler;
import cn.jfjb.crud.exception.UserNotExistException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/**
* @author john
* @date 2019/11/24 - 10:43
*/
@ControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(UserNotExistException.class)
public String handleException(Exception e, HttpServletRequest request) {
Map<String, Object> map = new HashMap<>();
//傳入我們自己的錯誤狀態(tài)碼 4xx 5xx,否則就不會進入定制錯誤頁面的解析流程
/**
* Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");
*/
request.setAttribute("javax.servlet.error.status_code", 400);
map.put("code", "user.notexist");
map.put("message", e.getMessage());
//轉發(fā)給錯誤處理器MyErrorAttributes
request.setAttribute("ext", map);
//轉發(fā)到/error進行自適應響應效果處理
return "forward:/error";
}
}
3. 定制數(shù)據(jù)攜帶出去
出現(xiàn)錯誤以后,會來到/error請求,會被BasicErrorController處理,響應出去可以獲取的數(shù)據(jù)是由getErrorAttributes得到的(是AbstractErrorController(ErrorController)規(guī)定的方法);
1、完全來編寫一個ErrorController的實現(xiàn)類【或者是編寫AbstractErrorController的子類】,放在容器中;
2、頁面上能用的數(shù)據(jù),或者是json返回能用的數(shù)據(jù)都是通過errorAttributes.getErrorAttributes得到;
容器中DefaultErrorAttributes.getErrorAttributes();默認進行數(shù)據(jù)處理的;
自定義ErrorAttributes
package cn.jfjb.crud.component;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;
import java.util.Map;
/**
* @author john
* @date 2019/11/24 - 12:13
*/
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
Map<String, Object> map = super.getErrorAttributes(webRequest, includeStackTrace);
//獲取自定義處理異常傳遞的參數(shù)
Map<String, Object> ext = (Map<String, Object>) webRequest.getAttribute("ext", 0);
map.put("company", "atguigu");
map.put("ext", ext);
return map;
}
}
4. 配置application.yml
server: error: include-exception: true
5. 編寫4xx.html自定義錯誤頁面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>4xx</title>
</head>
<body>
<h1>status:[[${status}]]</h1>
<h2>timestamp:[[${timestamp}]]</h2>
<h2>exception:[[${exception}]]</h2>
<h2>message:[[${message}]]</h2>
</body>
</html>
6. 測試
package cn.jfjb.crud.controller;
import cn.jfjb.crud.exception.UserNotExistException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author john
* @date 2019/11/22 - 19:38
*/
@Controller
public class HelloController {
@RequestMapping({"/testException"})
public String testException(@RequestParam("user") String user) {
if (user != "aaa") {
throw new UserNotExistException();
}
return "index";
}
}



以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java報錯:ClassCastException問題解決方法
異常是程序中的一些錯誤,但并不是所有的錯誤都是異常,并且錯誤有時候是可以避免的,下面這篇文章主要給大家介紹了關于Java報錯:ClassCastException問題解決方法,需要的朋友可以參考下2024-07-07
springBoot項目中的全局異常處理和自定義異常處理實現(xiàn)
異常是由于程序邏輯錯誤、運行環(huán)境問題、用戶輸入錯誤等原因導致的一種非正常的狀態(tài)或事件,本文主要介紹了springBoot項目中的全局異常處理和自定義異常處理實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-08-08

