企业级Spring学习日记(八)

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,"系统繁忙,请联系管理员!")
		}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值