SSM整合
表现层数据封装
- 前端接收数据格式:创建结果模型类,封装数据到data属性中
- 设置统一数据返回结果类
public class Result {
private Object data;
private Integer code;
private Stirng msg;
}
- 设置统一数据返回结果编码
public class code {
public static final Integer SAVE_OK = 20011;
public static final Integer DELETE_OK = 20021;
public static final Integer UPDATE_OK = 20031;
public static final Integer GET_OK = 20041;
public static final Integer SAVE_ERR = 20010;
public static final Integer DELETE_ERR = 20020;
public static final Integer UPDATE_ERR = 20030;
public static final Integer GET_ERR = 20040;
}
- 根据情况设定合理的Result
@RequestMapping("/books")
public class BookControler {
@Autowired
private BookService bookService;
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id) {
Book book = bookService.getById(id);
Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
String msg = book != null? "" :"数据查询错误,请重试!";
return new Result(code,book,msg);
}
}
异常处理器
各个层级均有可能出现异常,讲所有异常抛出到变现层进行处理,集中的、统一的处理项目中出现的异常
- 名称:@RestControllerAdvice
- 类型:类注解
- 位置:Rest风格开发的控制器增强类定义上方
- 作用:为Rest风格开发的控制器类做增强
- 范例:
@RestControllerAdvice
public class ProjectExceptionAdivice {
}
-
说明:此注解自带@ResponseBody注解与@Component注解,具备对应功能
-
名称ExcpetiontionHandler
-
类型:方法注解
-
位置:专用于异常处理控制器方法上方
-
作用:设置制定异常的处理方案,功能等同于控制器方法,出现异常后终止原始控制器执行,并转入当前方法执行
-
范例:
@RestControllerAdvice
public class ProjiectExceptionAdvice {
@ExceptionHandler(Exception.class)
public Result doException(Exception ex){
return new Result(666,null);
}
}
- 说明:此类方法可以根据处理异常不同,制作多个方法分贝处理对应场景
项目异常处理方案
- 项目异常分类
-业务异常(BusinessExceptin)
-系统异常(SystemException)
-其他异常(Exception)
①自定义系统级异常
public class SystemException extends RuntimeException{
private Integer code;
public SystemException(Integer code,String message) {
super(message);
this.code = code;
}
public SystemException(Integer code,String message,Throwable cause) {
super(message,cause);
this.code = code;
}
public Integer gerCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
②:自定义业务级异常
public class BusinessException extends RuntimeException{
private Integer code;
public BusinessException(Integer code,String message) {
super(message);
this.code = code;
}
public BusinessException(Integer code,String message,Throwable cause) {
super(message,cause);
this.code = code;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
③:自定义异常编码
public class Code {
public static final Integer SYSTEM_UNKNOW_ERROR = 50001;
public static final Integer SYSTEM_TIMEOUT_ERROR = 50002;
public static final Integer PROJECT_VALIDATE_ERROR = 60001;
public static final Integer PROJECT_ BUSINESS_ERROR = 60002;
}
④:触发自定义异常
@Service
public class BookServiceImpl implements BookSerive {
@Autowired
private BookDao bookDao;
public Book getById(Integer id) {
if(id < 0){
throw new BusinessException(Code.PORJECT_BUSINESS_ERROR,"请勿进行非法操作!");
}
return bookDao.getById(id)
}
}
⑤:拦截并处理异常
@RestControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(BusinessException.class)
public Result doBusinessException(BusinessException ex){
return new Result(ex.getCode(),null,ex.getMessage());
}
@ExceptionHandler(SystemException.calss)
public Result doSystemException(SystemException ex){
public Result doSystemException(ex){
// 记录日志
// 发送邮件给开发人员
// 发送短信给运维人员
return new Result(ex.getCode(),null,ex.getMessage());
}
@ExceptionHandler(Exception.class)
public Result doException (Exception ex){
// 记录日志
// 发送邮件给开发人员
// 发送短信给运维人员
return new Result(Code.SYSTEM_UNKNOW_ERROR,null,"系统繁忙,请联系管理员!")
}
}