springboot项目集成spring security+jwt
每次运行时抛出自定义异常时都会报空指针
/**
* 自定义异常
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonException extends RuntimeException {
private int code;
private String msg;
public CommonException(ResultCode resultCode) {
this.code = resultCode.getCode();
this.msg = resultCode.getMessage();
}
}
/**
* 自定义捕获
*/
@RestControllerAdvice
public class CommonExceptionHandler {
public static final Logger logger = LoggerFactory.getLogger(CommonExceptionHandler.class);
/**
* 全局异常捕捉处理
* @param ex
* @return
*/
@ExceptionHandler(value = Exception.class)
public R errorHandler(HttpServletRequest req, Exception ex) {
logger.error(String.format("发生未处理的异常={%s},url={%s},params={%s}",
ex.getMessage(),req.getRequestURL(),req.getParameterMap().toString()),ex);
return R.fail(100,ex.getMessage());
}
/**
* 拦截捕捉自定义异常 CommonException.class
* @param ex
* @return
*/
@ExceptionHandler(value = CommonException.class)
public Map myErrorHandler(HttpServletRequest req, CommonException ex) {
Map map = new HashMap();
map.put("code", ex.getCode());
map.put("msg", ex.getMsg());
return map;
}
}
/**
* 使用
*/
throw new CommonException(401, "token失效,请重新登录");