@RestControllerAdvice注解会应用到所有@RequestMapping中。
直接上代码吧
package com.example.jwt2021921.core;
import com.auth0.jwt.exceptions.TokenExpiredException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
@RestControllerAdvice
@Slf4j
public class GlobalException {
// 捕捉其他所有异常
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
public RestResponse globalException(Exception e) {
log.error("服务器异常:", e);
return RestResponse.failed("服务器异常",e.getMessage());
}
@ExceptionHandler(TokenExpiredException.class)
public RestResponse tokenExc(Exception e){
log.error("token异常:", e);
return RestResponse.failed("token异常",e.getMessage());
}
//参数验证
@ExceptionHandler(IllegalStateException.class)
public RestResponse IllegalStateException(IllegalStateException e) {
log.error("参数错误:", e);
return RestResponse.failed("参数错误",e.getMessage());
}
//参数绑定
@ExceptionHandler(BindException.class)
public RestResponse<String> BeanPropertyBindingResult(HttpServletRequest request, Throwable ex, BindException e) {
log.error("参数绑定失败:", e);
return RestResponse.failed(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
}
//运行时异常
@ExceptionHandler(RuntimeException.class)
public RestResponse<String> RuntimeException(RuntimeException e) {
log.error("运行时异常:", e);
return RestResponse.failed(e.getMessage());
}
//404处理
@ExceptionHandler(NoHandlerFoundException.class)
public RestResponse<String> NoHandlerFoundException(NoHandlerFoundException e) {
return RestResponse.failed("请求地址错误");
}
}
还有什么异常自己可以定义在类中。
RestResponse这个类是自己定义的,作用于controller中返回的类容
代码如下
package com.example.jwt2021921.core;
import lombok.*;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* 响应信息主体
*
* @param <T>
* @author ptyhzw
*/
//@ToString
//@Accessors(chain = true)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RestResponse<T> implements Serializable {
private static final long serialVersionUID = 1L;
@Getter
@Setter
//返回标记:成功标记=0,失败标记=1
private int code;
@Getter
@Setter
//返回信息
private String msg;
@Getter
@Setter
//数据
private T data;
public static <T> RestResponse<T> ok() {
return restResult(null, SystemConstant.SUCCESS, null);
}
public static <T> RestResponse<T> ok(T data) {
return restResult(data, SystemConstant.SUCCESS, null);
}
public static <T> RestResponse<T> ok(int code, String msg) {
return restResult(code, msg);
}
public static <T> RestResponse<T> ok(T data, String msg) {
return restResult(data, SystemConstant.SUCCESS, msg);
}
public static <T> RestResponse<T> failed() {
return restResult(null, SystemConstant.FAIL, null);
}
public static <T> RestResponse<T> failed(String msg) {
return restResult(null, SystemConstant.FAIL, msg);
}
public static <T> RestResponse<T> failed(T data) {
return restResult(data, SystemConstant.FAIL, null);
}
public static <T> RestResponse<T> failed(String msg,T data) {
return restResult(data, SystemConstant.FAIL, msg);
}
private static <T> RestResponse<T> restResult(T data, int code, String msg) {
RestResponse<T> apiResult = new RestResponse<>();
apiResult.setCode(code);
apiResult.setData(data);
apiResult.setMsg(msg);
return apiResult;
}
private static <T> RestResponse<T> restResult(int code, String msg) {
RestResponse<T> apiResult = new RestResponse<>();
apiResult.setCode(code);
apiResult.setMsg(msg);
return apiResult;
}
}
package com.example.jwt2021921.core;
public final class SystemConstant {
/**
* 成功标记
*/
public static final Integer SUCCESS = 0;
/**
* 失败标记
*/
public static final Integer FAIL = 1;
}
@RestControllerAdvice 是Java中用于全局异常处理的注解,它可以覆盖所有的@RequestMapping注解的方法。通过它,开发者可以自定义异常处理逻辑,统一返回格式。在本文中,将展示如何使用@RestControllerAdvice进行异常处理,并定义自定义的RestResponse类来规范控制器的返回内容。
1512

被折叠的 条评论
为什么被折叠?



