文章积累知识,如有存在问题,请大家不啬赐教
1、项目异常统一管理
(1)采用枚举类统一管理异常信息,
package com.web.utils;
public enum ExceptionEnum{
/**
* 请发送验证码
*/
VERIFICATION_CODE_NOT_SENT("5001", "请发送验证码"),
/**
* 您输入的验证码有误
*/
VERIFICATION_CODE_ERROR("5002", "您输入的验证码有误"),
//自定义的状态码
DATABASE_EXCEPTION("2000", "数据库连接异常"),
MONITOR_EXCEPTION("2001", "文件监控异常"),
SERVER_IS_BUSY("2003", "服务器正忙...");
//错误码
public String code;
//提示信息
public String message;
//构造函数
ExceptionEnum(String code, String message) {
this.code = code;
this.message = message;
}
//获取状态码
public String getCode() {
return code;
}
//获取提示信息
public String getMessage() {
return message;
}
}
(2)采用自定义异常处理了机制
//此处异常类得名字建议用项目名加上Exception,整个项目的通用异常
public class FruitsException extends RuntimeException {
private ExceptionEnum exceptionEnum;
/*
* 自定义异常信息
*/
private String errorDetail;
/**
* 带自定义异常信息的构造方法
* @param exceptionEnums
* @param errorDetail
*/
public FruitsException(ExceptionEnum exceptionEnums,String errorDetail){
this.exceptionEnum = exceptionEnums;
this.errorDetail = errorDetail;
}
public FruitsException(ExceptionEnum exceptionEnum) {
this.exceptionEnum = exceptionEnum;
}
public ExceptionEnum getExceptionEnum() {
return exceptionEnum;
}
public String getErrorDetail() {
return errorDetail;
}
public void setErrorDetail(String errorDetail) {
this.errorDetail = errorDetail;
}
//用来输出异常信息和状态码
public void printException(FruitsException e) {
ExceptionEnum exceptionEnum = e.getExceptionEnum();
System.out.println("异常代码:" + exceptionEnum.getCode() + ",异常信息:" + exceptionEnum.getMessage());
}
}
2、service 业务逻辑层,数据回滚(很重要),并抛出异常,待controller捕获并处理
@Override
@Transactional(readOnly = false)
public Map<String, Object> toPayBySmall(TblOrder record) throws Exception {
TblOrder order = tblOrderMapper.selectByPrimaryKey(record.getId());
// 订单不存在
if (null == order) {
throw new FruitsException(ExceptionEnum.ORDER_CANCEL_OR_SHIPPED);
}
// 订单已过期
int orderStatus = Integer.parseInt(order.getOrderStatus());
if (orderStatus == 8) {
throw new FruitsException(ExceptionEnum.ORDER_EXPIRED);
}
// 订单已支付
if (orderStatus > 0) {
throw new FruitsException(ExceptionEnum.ORDER_HAS_BEEN_PAID);
}
// 用户不存在openId
TblManager manager = managerMapper.selectByPrimaryKey(order.getManagerId());
String openid = manager.getOpenid();
if (StringUtils.isEmpty(openid)) {
throw new FruitsException(ExceptionEnum.USER_DOES_NOT_HAVE_OPENID);
}
// 商品总价
String goodsTotalPrice = new BigDecimal(order.getGoodsCount()).multiply(new BigDecimal(order.getGoodsAgentPrice())).toString();
Map<String, Object> jsSdkMap = PayMoneyUtil.smallPay(order.getOrderNum(), openid, Integer.parseInt(goodsTotalPrice));
// 调用支付失败
if (jsSdkMap == null) {
throw new FruitsException(ExceptionEnum.ABNORMAL_PAYMENT_PARAMETERS);
}
return jsSdkMap;
3、controller 服务应用层,捕获异常,并处理异常,返回数据
/**
* 小程序-统一下单
*
* @param record
* @return
* @author tsy
*/
@RequestMapping(value = "/toPayBySmall", method = RequestMethod.POST)
@ResponseBody
public Result pay(@RequestBody TblOrder record) {
try {
Map<String, Object> map = tblOrderService.toPayBySmall(record);
return new Result(true, map);
} catch (FruitsException e) {
return new Result(false, e.getExceptionEnum().message);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, "支付失败");
}
}
后记:
文章的代码是我平时工作中采用这样的方式,倘若有幸被您看到文章,并能提出不同的意见,将不胜感激,旨在提高自己的代码能力