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

異常分為以下三種
- 自定義異常
- 可預(yù)知異常
- 不可預(yù)知異常
下面具體說(shuō)明如何分類處理,從而保證無(wú)論觸發(fā)什么異常均可返回理想的自定義數(shù)據(jù)格式
ResultCode
/**
* Created by mrt on 2018/3/5.
* 10000-- 通用錯(cuò)誤代碼
* 22000-- 媒資錯(cuò)誤代碼
* 23000-- 用戶中心錯(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);
}
}自定義異常類
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ù)枚舉類)
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存放異常類型和錯(cuò)誤代碼的映射,ImmutableMap的特點(diǎn)的一旦創(chuàng)建不可改變,并且線程安全
private static ImmutableMap<Class<? extends Throwable>, ResultCode> EXCEPTIONS;
//使用builder來(lái)構(gòu)建一個(gè)異常類型和錯(cuò)誤代碼的異常
protected static ImmutableMap.Builder<Class<? extends Throwable>, ResultCode> builder =
ImmutableMap.builder();
static {
//在這里加入一些基礎(chǔ)的異常類型判斷---異常存在非自定義異常
// 參數(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)文章
Spring Boot 多個(gè)定時(shí)器沖突問(wèn)題的解決方法
這篇文章主要介紹了Spring Boot 多個(gè)定時(shí)器沖突問(wèn)題的解決方法,實(shí)際開(kāi)發(fā)中定時(shí)器需要解決多個(gè)定時(shí)器同時(shí)并發(fā)的問(wèn)題,也要解決定時(shí)器之間的沖突問(wèn)題,本文通過(guò)問(wèn)題場(chǎng)景重現(xiàn)給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-05-05
SpringBoot集成nacos動(dòng)態(tài)刷新數(shù)據(jù)源的實(shí)現(xiàn)示例
這篇文章主要介紹了SpringBoot集成nacos動(dòng)態(tài)刷新數(shù)據(jù)源的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
springboot搭建訪客管理系統(tǒng)的實(shí)現(xiàn)示例
這篇文章主要介紹了springboot搭建訪客管理系統(tǒng)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java中的日期和時(shí)間類以及Calendar類用法詳解
這篇文章主要介紹了Java中的日期和時(shí)間類以及Calendar類用法詳解,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
Java實(shí)現(xiàn)創(chuàng)建運(yùn)行時(shí)類的對(duì)象操作示例
這篇文章主要介紹了Java實(shí)現(xiàn)創(chuàng)建運(yùn)行時(shí)類的對(duì)象操作,結(jié)合實(shí)例形式分析了Java動(dòng)態(tài)創(chuàng)建對(duì)象的原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-08-08
基于SpringBoot實(shí)現(xiàn)七天免登錄的完整流程
作為一名Java后端高級(jí)開(kāi)發(fā),我敢說(shuō)七天免登錄是業(yè)務(wù)系統(tǒng)里最常見(jiàn)的需求之一,這個(gè)需求看似簡(jiǎn)單,但實(shí)現(xiàn)不好很容易踩坑:要么免登錄失效影響用戶體驗(yàn),要么出現(xiàn)安全漏洞導(dǎo)致賬號(hào)被盜,今天這篇文章,我就結(jié)合實(shí)際工作經(jīng)驗(yàn),講透七天免登錄的標(biāo)準(zhǔn)實(shí)現(xiàn)方案2026-01-01
Maven?項(xiàng)目用Assembly打包可執(zhí)行jar包的方法
這篇文章主要介紹了Maven?項(xiàng)目用Assembly打包可執(zhí)行jar包的方法,該方法只可打包非spring項(xiàng)目的可執(zhí)行jar包,需要的朋友可以參考下2023-03-03

