自定义异常
示例代码
import com.imooc.order.converter.OrderForm2OrderDtoConverter;
import com.imooc.order.dto.OrderDto;
import com.imooc.order.enums.ResultEnum;
import com.imooc.order.exception.OrderException;
import com.imooc.order.form.OrderForm;
import com.imooc.order.service.OrderService;
import com.imooc.order.utils.ResultVoUtil;
import com.imooc.order.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* 订单操作controller
*/
@RestController
@RequestMapping(value = "order")
@Slf4j
public class OrderController {
@Autowired
private OrderService orderService;
@PostMapping("create")
public ResultVo<Map<String, String>> create(OrderForm orderForm, BindingResult bindingResult) {
// 校验参数,如果传递错误就抛出异常
if (bindingResult.hasErrors()) {
log.error("参数传递错误,orderForm={}", orderForm);
throw new OrderException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage());
}
// 转换参数
OrderDto orderDto = OrderForm2OrderDtoConverter.conver(orderForm);
if (CollectionUtils.isEmpty(orderDto.getDetailList())) {
log.error("购物车信息不能为空,String={}", orderDto.getDetailList());
throw new OrderException(ResultEnum.CART_ENPTY);
}
OrderDto dto = orderService.create(orderDto);
Map<String, Object> map = new HashMap<>();
map.put("id", dto.getOrderId());
return ResultVoUtil.success(map);
}
}
转换参数类 OrderForm2OrderDtoConverter
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.imooc.order.dataobject.OrderDetail;
import com.imooc.order.dto.OrderDto;
import com.imooc.order.enums.ResultEnum;
import com.imooc.order.exception.OrderException;
import com.imooc.order.form.OrderForm;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
/**
* 对象互相转换
*/
@Slf4j
public class OrderForm2OrderDtoConverter {
public static OrderDto conver(OrderForm orderForm) {
Gson gson = new Gson();
OrderDto orderDto = new OrderDto();
orderDto.setBuyerName(orderForm.getName());
orderDto.setBuyerPhone(orderForm.getPhone());
orderDto.setBuyerOpenid(orderForm.getOpenId());
orderDto.setBuyerAddress(orderForm.getAddress());
List<OrderDetail> list = new ArrayList<>();
// 将订单详情的json字符串转为list
try {
list = gson.fromJson(orderForm.getItems(), new TypeToken<List<OrderDetail>>() {
}.getType());
} catch (Exception e) {
log.error("订单详情参数转换错误,String={}", orderForm.getItems());
// 抛出异常
throw new OrderException(ResultEnum.PARAM_ERROR);
}
orderDto.setDetailList(list);
return orderDto;
}
}
上面的代码中如果再参数传递错误或查询无数据时我们可以自定义,让他抛出异常
1.先创建异常枚举类(定义相关异常描述和异常码)
@Getter
public enum ResultEnum {
/**
* 参数错误
*/
PARAM_ERROR(1,"参数错误"),
/**
* 购物车不能为空
*/
CART_ENPTY(2,"购物车不能为空"),
;
private Integer code;
private String message;
ResultEnum(int code, String message) {
this.code = code;
this.message = message;
}
}
2.创建异常类,继承RuntimeException。添加相应的构造方法
public class OrderException extends RuntimeException {
private Integer code;
public OrderException(Integer code, String message){
super(message);
this.code = code;
}
public OrderException(ResultEnum resultEnum){
super(resultEnum.getMessage());
this.code = resultEnum.getCode();
}
}
异常的枚举类中可以进行扩展异常信息