@ControllerAdvice
public class DefaultExceptionHandler {
/**
* 没有权限 异常
*/
@ExceptionHandler({UnauthorizedException.class})
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public ResponseBean processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) {
return new ResponseBean(ResponseBean.FAIL,"您没有权限");
}
}
代码如上 下面是我的ResponseBean的构造方法
public ResponseBean() {
super();
}
public ResponseBean(String code, String msg) {//讲道理应该调用此构造方法
super();
this.code = code;
this.msg = msg;
}
public ResponseBean(String code, String msg, List<?> data) {
super();
this.code = code;
this.msg = msg;
this.data = data;
}
public ResponseBean(String code,String msg,List<?> data,int count){//不讲道理的调用了此方法
super();
this.code = code;
this.msg = msg;
this.data = data;
this.count=count;
}
当前返回的json格式如下
{"code":"1","count":0,"msg":"您没有权限"}
没有吧,应该是 count 有 setter/getter 方法,并且 count 的int 默认值是 0