DefaultExceptionAdvice
使用ControllerAdvice使其生效
package com.xx.common.exception;
import com.xx.common.core.domain.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import java.nio.file.AccessDeniedException;
import java.sql.SQLException;
/**
* @author zihang on 2020/4/5 21:38
*/
@Slf4j
@RestController
public class DefaultExceptionAdvice {
/**
* IllegalArgumentException异常处理返回json
* 返回状态码:400
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({IllegalArgumentException.class})
public Result<Void> badRequestException(IllegalArgumentException e) {
return defHandler("参数解析失败", e);
}
/**
* AccessDeniedException异常处理返回json
* 返回状态码:403
*/
@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler({AccessDeniedException.class})
public Result<Void> badMethodExpressException(AccessDeniedException e) {
return defHandler("没有权限请求当前方法", e);
}
/**
* 返回状态码:405
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler({HttpRequestMethodNotSupportedException.class})
public Result<Void> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
return defHandler("不支持当前请求方法", e);
}
/**
* 返回状态码:415
*/
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler({HttpMediaTypeNotSupportedException.class})
public Result<Void> handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
return defHandler("不支持当前媒体类型", e);
}
/**
* 返回状态码:500
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler({SQLException.class})
public Result<Void> handleSQLException(SQLException e) {
return defHandler("服务运行SQLException异常", e);
}
/**
* BusinessException 业务异常处理
* 返回状态码:500
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(BusinessException.class)
public Result<Void> handleException(BusinessException e) {
return defHandler("业务异常", e);
}
/**
* 所有异常统一处理
* 返回状态码:500
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public Result<Void> handleException(Exception e) {
return defHandler("未知异常", e);
}
private Result<Void> defHandler(String msg, Exception e) {
log.error(msg, e);
return Result.failed(msg);
}
}
返回的是json格式,注解ResponseStatus无效果
Result
package com.xx.common.core.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @author zihang on 2020/4/5 20:01
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {
private Integer code;
private String msg;
private T data;
public static <T> Result<T> ok(String msg) {
return okWith(null, CodeEnum.SUCCESS.getCode(), msg);
}
public static <T> Result<T> ok(T model, String msg) {
return okWith(model, CodeEnum.SUCCESS.getCode(), msg);
}
public static <T> Result<T> ok(T model) {
return okWith(model, CodeEnum.SUCCESS.getCode(), "");
}
public static <T> Result<T> okWith(T data, Integer code, String msg) {
return new Result<>(code, msg, data);
}
public static <T> Result<T> failed(String msg) {
return failedWith(CodeEnum.ERROR.getCode(), msg, null);
}
public static <T> Result<T> failed(T model, String msg) {
return failedWith(CodeEnum.ERROR.getCode(), msg, model);
}
public static <T> Result<T> failedWith(Integer code, String msg, T data) {
return new Result<>(code, msg, data);
}
@Getter
enum CodeEnum {
SUCCESS(200),
ERROR(-1);
private Integer code;
CodeEnum(Integer code) {
this.code = code;
}
}
}