欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot中500錯(cuò)誤的問(wèn)題解決

 更新時(shí)間:2025年11月07日 09:29:36   作者:智能創(chuàng)新者  
本文詳細(xì)介紹了Spring Boot中處理500錯(cuò)誤的幾種方法,包括自定義異常、可預(yù)知異常和不可預(yù)知異常的分類(lèi)處理,具有一定的參考價(jià)值,感興趣的可以了解一下

本文詳細(xì)的介紹了springboot 500錯(cuò)誤的幾種問(wèn)題解決

異常分為以下三種

  • 自定義異常
  • 可預(yù)知異常
  • 不可預(yù)知異常

下面具體說(shuō)明如何分類(lèi)處理,從而保證無(wú)論觸發(fā)什么異常均可返回理想的自定義數(shù)據(jù)格式

ResultCode

/**
 * Created by mrt on 2018/3/5.
 * 10000-- 通用錯(cuò)誤代碼
 * 22000-- 媒資錯(cuò)誤代碼
 * 23000-- 用戶(hù)中心錯(cuò)誤代碼
 * 24000-- cms錯(cuò)誤代碼
 * 25000-- 文件系統(tǒng)
 */
public interface ResultCode {
    //操作是否成功,true為成功,false操作失敗
    boolean success();
    //操作代碼
    int code();
    //提示信息
    String message();

}

ResponseResult

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@ToString
@NoArgsConstructor
public class ResponseResult implements Response {

    //操作是否成功
    boolean success = SUCCESS;

    //操作代碼
    int code = SUCCESS_CODE;

    //提示信息
    String message;

    public ResponseResult(ResultCode resultCode) {
        this.success = resultCode.success();
        this.code = resultCode.code();
        this.message = resultCode.message();
    }

    public static ResponseResult SUCCESS() {
        return new ResponseResult(CommonCode.SUCCESS);
    }

    public static ResponseResult FAIL() {
        return new ResponseResult(CommonCode.FAIL);
    }

}

自定義異常類(lèi)

import com.xuecheng.framework.model.response.ResultCode;

/**
 * 自定義異常
 *
 * @author john
 * @date 2019/12/20 - 10:57
 */
public class CustomException extends RuntimeException {
    private ResultCode resultCode;

    public CustomException(ResultCode resultCode) {
        //異常信息為錯(cuò)誤代碼+異常信息
        super("錯(cuò)誤代碼:" + resultCode.code() + "錯(cuò)誤信息:" + resultCode.message());
        this.resultCode = resultCode;
    }

    public ResultCode getResultCode() {
        return this.resultCode;
    }
}

CommonCode (自定義異常信息返回?cái)?shù)據(jù)枚舉類(lèi))

import lombok.ToString;

@ToString
public enum CommonCode implements ResultCode{
    INVALID_METHOD(false,10004,"非法方法!"),
    INVALID_PARAM(false,10003,"非法參數(shù)!"),
    SUCCESS(true,10000,"操作成功!"),
    FAIL(false,11111,"操作失?。?),
    UNAUTHENTICATED(false,10001,"此操作需要登陸系統(tǒng)!"),
    UNAUTHORISE(false,10002,"權(quán)限不足,無(wú)權(quán)操作!"),
    SERVER_ERROR(false,99999,"抱歉,系統(tǒng)繁忙,請(qǐng)稍后重試!");
//    private static ImmutableMap<Integer, CommonCode> codes ;
    //操作是否成功
    boolean success;
    //操作代碼
    int code;
    //提示信息
    String message;
    private CommonCode(boolean success,int code, String message){
        this.success = success;
        this.code = code;
        this.message = message;
    }

    @Override
    public boolean success() {
        return success;
    }
    @Override
    public int code() {
        return code;
    }

    @Override
    public String message() {
        return message;
    }


}

異常處理(包含自定義異常和不可預(yù)見(jiàn)異常和可預(yù)見(jiàn)異常)

import com.google.common.collect.ImmutableMap;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.framework.model.response.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author john
 * @date 2019/12/20 - 11:01
 */
@Slf4j
@ControllerAdvice
public class ExceptionCatch {
    //使用EXCEPTIONS存放異常類(lèi)型和錯(cuò)誤代碼的映射,ImmutableMap的特點(diǎn)的一旦創(chuàng)建不可改變,并且線(xiàn)程安全
    private static ImmutableMap<Class<? extends Throwable>, ResultCode> EXCEPTIONS;
    //使用builder來(lái)構(gòu)建一個(gè)異常類(lèi)型和錯(cuò)誤代碼的異常
    protected static ImmutableMap.Builder<Class<? extends Throwable>, ResultCode> builder =
            ImmutableMap.builder();

    static {
        //在這里加入一些基礎(chǔ)的異常類(lèi)型判斷---異常存在非自定義異常
        // 參數(shù)異常
        // 請(qǐng)求方法異常
        builder.put(HttpMessageNotReadableException.class, CommonCode.INVALID_PARAM);
        builder.put(HttpRequestMethodNotSupportedException.class, CommonCode.INVALID_METHOD);
    }


    //捕獲   CustomException異常
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public ResponseResult customException(CustomException e) {
        log.error("catch   exception   :   {}\r\nexception:   ", e.getMessage(), e);
        ResultCode resultCode = e.getResultCode();
        return new ResponseResult(resultCode);
    }


    //捕獲Exception異常---非自定義異常
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseResult exception(Exception e) {
        log.error("catch   exception   :   {}\r\nexception:   ", e.getMessage(), e);
        if (EXCEPTIONS == null)
            EXCEPTIONS = builder.build();
        // 查看異常是否被定義返回格式---沒(méi)有就返回默認(rèn)錯(cuò)誤格式
        final ResultCode resultCode = EXCEPTIONS.get(e.getClass());
        final ResponseResult responseResult;
        if (resultCode != null) {
            responseResult = new ResponseResult(resultCode);
        } else {
            responseResult = new ResponseResult(CommonCode.SERVER_ERROR);
        }
        return responseResult;
    }
}

到此這篇關(guān)于springboot 500錯(cuò)誤的問(wèn)題解決的文章就介紹到這了,更多相關(guān)springboot 500錯(cuò)誤內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java Web用戶(hù)登錄實(shí)例代碼

    Java Web用戶(hù)登錄實(shí)例代碼

    這篇文章主要介紹了Java Web用戶(hù)登錄實(shí)例代碼的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起看看吧
    2016-05-05
  • IntelliJ IDEA中程序包org.slf4j找不到的解決

    IntelliJ IDEA中程序包org.slf4j找不到的解決

    這篇文章主要介紹了IntelliJ IDEA中程序包org.slf4j找不到的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Spring(一):IOC如何推導(dǎo)和理解

    Spring(一):IOC如何推導(dǎo)和理解

    下面小編就為大家?guī)?lái)一篇詳談Spring對(duì)IOC的理解(推薦篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-07-07
  • springboot定時(shí)任務(wù)詳解

    springboot定時(shí)任務(wù)詳解

    這篇文章主要介紹了springboot定時(shí)任務(wù)的相關(guān)資料,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2021-01-01
  • SpringBoot解析LocalDateTime失?。篣niapp傳輸時(shí)間變1970的原因與解決方案

    SpringBoot解析LocalDateTime失?。篣niapp傳輸時(shí)間變1970的原因與解決方案

    這篇文章主要介紹了SpringBoot解析LocalDateTime失???Uniapp傳輸時(shí)間變1970的原因與解決方案,文中通過(guò)代碼示例給大家講解的非常詳細(xì),需要的朋友可以參考下
    2025-03-03
  • idea+ springboot熱部署的配置方法

    idea+ springboot熱部署的配置方法

    這篇文章主要介紹了idea+ springboot熱部署的配置方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Spring之ORM模塊代碼詳解

    Spring之ORM模塊代碼詳解

    這篇文章主要介紹了Spring之ORM模塊代碼詳解,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • Java參數(shù)按值傳遞示例演示

    Java參數(shù)按值傳遞示例演示

    在Java中,方法參數(shù)的傳遞方式實(shí)際上是按值傳遞,接下來(lái)通過(guò)本文給大家介紹了Java參數(shù)按值傳遞示例演示,需要的朋友可以參考下
    2023-09-09
  • java常用的加密解決方案分享

    java常用的加密解決方案分享

    這篇文章全面介紹了Java中加解密技術(shù)的應(yīng)用,包括哈希函數(shù)、對(duì)稱(chēng)加密、非對(duì)稱(chēng)加密、消息認(rèn)證碼和數(shù)字簽名等,它詳細(xì)解釋了每種技術(shù)的工作原理,并提供了相應(yīng)的Java代碼示例,文章還強(qiáng)調(diào)了密鑰管理的重要性,并提出了在實(shí)際應(yīng)用中遵循的最佳實(shí)踐
    2025-01-01
  • Springboot Apollo配置yml的問(wèn)題及解決方案

    Springboot Apollo配置yml的問(wèn)題及解決方案

    這篇文章主要介紹了Springboot Apollo配置yml的問(wèn)題及解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06

最新評(píng)論