最快捷的统一异常处理
方法/步骤
package entity;
public class Result {
private boolean flag;//是否成功
private Integer code;// 返回码
private String message;//返回信息
private Object data;// 返回数据
//省略Get / Set
}
package entity;
import java.util.List;
public class PageResult<T> {
private Long total;
private List<T> rows;
//省略Get / Set
}
package entity;
public class StatusCode {
public static final int OK=20000;//成功
public static final int ERROR =20001;//失败
public static final int LOGINERROR =20002;//用户名或密码错误
public static final int ACCESSERROR =20003;//权限不足
public static final int REMOTEERROR =20004;//远程调用失败
public static final int REPERROR =20005;//重复操作
}
@RestControllerAdvice
public class BaseExceptionHandler {
@ExceptionHandler(value = Exception.class)
public Result exception(Exception e){
e.printStackTrace();
return new Result(false, StatusCode.ERROR,e.getMessage(),"");
}
}
@RestController
@CrossOrigin
@RequestMapping("/label")
public class LabelController {
@Autowired
private LabelService labelService;
@RequestMapping(method = RequestMethod.GET)
public Result findAll(){
return new Result(true, StatusCode.OK, "查询成功",labelService.findAll());
}
}
参考文档