SpringBoot如何优雅的处理所有的异常接口返回值为JSON
前言
接口代码异常
当调用接口后,执行的代码发生了异常,通常就会返回如下页面
通过配置后,可以对所有的异常接口进行处理,返回如下的Json返回子更加美观,也方便查找错误
格式化后
接口不存在
当访问的接口不存在时,配置后将返回如下页面
如何配置获取所有异常并处理
1.application.properties配置
# 自定义
#出现错误时, 直接抛出异常
spring.mvc.throw-exception-if-no-handler-found=true
#不要为我们工程中的资源文件建立映射
spring.web.resources.add-mappings=false
2.编写异常处理类
package com.mabo.exception;
import com.mabo.core.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 统一异常处理类
*/
@Slf4j
@RestControllerAdvice
public class GlobalException {
@ExceptionHandler(Exception.class)
@ResponseBody
public Result doBusinessException(Exception exception) {
//日志记录堆栈错误信息
log.error(exception.getMessage(),exception);
// 接口异常捕获
return Result.fail("失败原因: "+ exception.getMessage());
}
}
3.统一返回封装类Result
package com.mabo.core;
import java.io.Serializable;
public class Result<T> implements Serializable {
private static final long serialVersionUID = 5778573516446596671L;
private static int SUCCESS = 0;
private static int FAIL = -1;
private static String MSG_SUCCESS = "成功";
private static String MSG_WARNING = "成功但有告警";
private static String MSG_FAIL = "失败";
private int code = 0;
private String type;
private String message;
private T data;
public Result() {
}
public Result(int code, String type, String message, T data) {
this.code = code;
this.type = type;
this.message = message;
this.data = data;
}
public static <T> Result<T> success(T data) {
return new Result(SUCCESS, ResponseType.TYPE_SUCCESS.getType(), MSG_SUCCESS, data);
}
public static <T> Result<T> success(String message, T data) {
message = message != null && message.length() > 0 ? message : MSG_SUCCESS;
return new Result(SUCCESS, ResponseType.TYPE_SUCCESS.getType(), message, data);
}
public static <T> Result<T> info(int code, String message, T data) {
message = message != null && message.length() > 0 ? message : MSG_SUCCESS;
return new Result(code, ResponseType.TYPE_INFO.getType(), message, data);
}
public static <T> Result<T> warning(int code, String message, T data) {
message = message != null && message.length() > 0 ? message : MSG_WARNING;
return new Result(code, ResponseType.TYPE_WARNING.getType(), message, data);
}
public static <T> Result<T> error(int code, String message, T data) {
message = message != null && message.length() > 0 ? message : MSG_FAIL;
return new Result(code, ResponseType.TYPE_ERROR.getType(), message, data);
}
public static <T> Result<T> fail(T data) {
return new Result(FAIL, ResponseType.TYPE_ERROR.getType(), MSG_FAIL, data);
}
public static <T> Result<T> fail(String message, T data) {
message = message != null && message.length() > 0 ? message : MSG_FAIL;
return new Result(FAIL, ResponseType.TYPE_ERROR.getType(), message, data);
}
public int getCode() {
return this.code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
protected static enum ResponseType {
TYPE_SUCCESS("success"),
TYPE_INFO("info"),
TYPE_WARNING("warning"),
TYPE_ERROR("error");
private String type;
private ResponseType(String type) {
this.type = type;
}
public String getType() {
return this.type;
}
}
}
4.如何验证
编写controller,启动项目,访问 http://localhost:port/test
就可以看到效果了
或者访问不存在的地址,也可以效果 ,例如 http://localhost:port/aaa
package com.mabo.controller;
import com.mabo.core.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class TestController {
// http://localhost:8081/test?id=1
@RequestMapping("/test")
public Result test(){
int i = 1 / 0;
return Result.success("test");
}
}