SpringBoot統一リターンパラメータと例外処理
戻るパッケージタイプ
package com.example.demo.config;
import lombok.Data;
import java.io.Serializable;
import java.util.HashMap;
/**
* lombok格式化数据
*/
@Data
public class BaseResult implements Serializable{
//返回码 非0即失败
private int code;
//消息提示
private String msg;
//返回的数据
private Object data;
public BaseResult(){};
public BaseResult(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public static BaseResult success() {
return success(new HashMap(0));
}
public static BaseResult success(Object data) {
return new BaseResult(0, "请求成功", data);
}
public static BaseResult failed() {
return failed("请求失败");
}
public static BaseResult failed(String msg) {
return failed(-1, msg);
}
public static BaseResult failed(int code, String msg) {
return new BaseResult(code, msg, new HashMap(0));
}
}
Exceptionクラス
package com.example.demo.config;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.IOException;
/**
* @Description 全局异常拦截器
*/
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
//运行时异常
@ExceptionHandler(RuntimeException.class)
public BaseResult runtimeExceptionHandler(RuntimeException ex) {
return resultFormat(1, ex);
}
//空指针异常
@ExceptionHandler(NullPointerException.class)
public BaseResult nullPointerExceptionHandler(NullPointerException ex) {
return resultFormat(2, ex);
}
//类型转换异常
@ExceptionHandler(ClassCastException.class)
public BaseResult classCastExceptionHandler(ClassCastException ex) {
return resultFormat(3, ex);
}
//IO异常
@ExceptionHandler(IOException.class)
public BaseResult iOExceptionHandler(IOException ex) {
return resultFormat(4, ex);
}
//未知方法异常
@ExceptionHandler(NoSuchMethodException.class)
public BaseResult noSuchMethodExceptionHandler(NoSuchMethodException ex) {
return resultFormat(5, ex);
}
//数组越界异常
@ExceptionHandler(IndexOutOfBoundsException.class)
public BaseResult indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException ex) {
return resultFormat(6, ex);
}
//400错误
@ExceptionHandler({HttpMessageNotReadableException.class})
public BaseResult requestNotReadable(HttpMessageNotReadableException ex) {
System.out.println("400..requestNotReadable");
return resultFormat(7, ex);
}
//400错误
@ExceptionHandler({TypeMismatchException.class})
public BaseResult requestTypeMismatch(TypeMismatchException ex) {
System.out.println("400..TypeMismatchException");
return resultFormat(8, ex);
}
//400错误
@ExceptionHandler({MissingServletRequestParameterException.class})
public BaseResult requestMissingServletRequest(MissingServletRequestParameterException ex) {
System.out.println("400..MissingServletRequest");
return resultFormat(9, ex);
}
//405错误
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
public BaseResult request405(HttpRequestMethodNotSupportedException ex) {
return resultFormat(10, ex);
}
//406错误
@ExceptionHandler({HttpMediaTypeNotAcceptableException.class})
public BaseResult request406(HttpMediaTypeNotAcceptableException ex) {
System.out.println("406...");
return resultFormat(11, ex);
}
//500错误
@ExceptionHandler({ConversionNotSupportedException.class, HttpMessageNotWritableException.class})
public BaseResult server500(RuntimeException ex) {
System.out.println("500...");
return resultFormat(12, ex);
}
//栈溢出
@ExceptionHandler({StackOverflowError.class})
public BaseResult requestStackOverflow(StackOverflowError ex) {
return resultFormat(13, ex);
}
//除数不能为0
@ExceptionHandler({ArithmeticException.class})
public BaseResult arithmeticException(ArithmeticException ex) {
return resultFormat(13, ex);
}
//其他错误
@ExceptionHandler({Exception.class})
public BaseResult exception(Exception ex) {
return resultFormat(14, ex);
}
private <T extends Throwable> BaseResult resultFormat(Integer code, T ex) {
ex.printStackTrace();
return BaseResult.failed(code, ex.getMessage());
}
}
テストインタフェース
package com.example.demo.controller;
import com.example.demo.config.BaseResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CaculatorController {
@GetMapping("/div")
@ResponseBody
public BaseResult div(int x, int y ){
return BaseResult.success(x/y);
}
}
このインターフェースにアクセスするためのプロジェクトを実行した後、リターンに成功2 1試験で割りました。
{
"code": 0,
"msg": "请求成功",
"data": 2
}
2試験で割っ0を返すことができませんでした。
{
"code": 13,
"msg": "/ by zero",
"data": {}
}