欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Springboot2.0自適應(yīng)效果錯誤響應(yīng)的示例分析

這篇文章主要介紹了Springboot2.0自適應(yīng)效果錯誤響應(yīng)的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)主營興海網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app軟件定制開發(fā),興海h5微信小程序搭建,興海網(wǎng)站營銷推廣歡迎興海等地區(qū)企業(yè)咨詢

實(shí)現(xiàn)效果當(dāng)訪問thymeleaf渲染頁面時,顯示的是自定義的錯誤頁面

當(dāng)以接口方式訪問時,顯示的是自定義的json數(shù)據(jù)響應(yīng)

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ù),轉(zhuǎn)發(fā)到/error進(jìn)行自適應(yīng)響應(yīng)效果處理

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,否則就不會進(jìn)入定制錯誤頁面的解析流程
    /**
     * 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());

    //轉(zhuǎn)發(fā)給錯誤處理器MyErrorAttributes
    request.setAttribute("ext", map);
    //轉(zhuǎn)發(fā)到/error進(jìn)行自適應(yīng)響應(yīng)效果處理
    return "forward:/error";
  }
}

3. 定制數(shù)據(jù)攜帶出去

出現(xiàn)錯誤以后,會來到/error請求,會被BasicErrorController處理,響應(yīng)出去可以獲取的數(shù)據(jù)是由getErrorAttributes得到的(是AbstractErrorController(ErrorController)規(guī)定的方法);

1、完全來編寫一個ErrorController的實(shí)現(xiàn)類【或者是編寫AbstractErrorController的子類】,放在容器中;

2、頁面上能用的數(shù)據(jù),或者是json返回能用的數(shù)據(jù)都是通過errorAttributes.getErrorAttributes得到;

容器中DefaultErrorAttributes.getErrorAttributes();默認(rèn)進(jìn)行數(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>
<h2>status:[[${status}]]</h2>
<h3>timestamp:[[${timestamp}]]</h3>
<h3>exception:[[${exception}]]</h3>
<h3>message:[[${message}]]</h3>
</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";
  }
}

Springboot2.0自適應(yīng)效果錯誤響應(yīng)的示例分析

Springboot2.0自適應(yīng)效果錯誤響應(yīng)的示例分析

Springboot2.0自適應(yīng)效果錯誤響應(yīng)的示例分析

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Springboot2.0自適應(yīng)效果錯誤響應(yīng)的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

新聞標(biāo)題:Springboot2.0自適應(yīng)效果錯誤響應(yīng)的示例分析
標(biāo)題來源:http://chinadenli.net/article36/gojhsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊品牌網(wǎng)站建設(shè)靜態(tài)網(wǎng)站服務(wù)器托管全網(wǎng)營銷推廣網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營