同业务多分支的接口设计
场景说明
比如处于下游的系统,在接收源系统的计划订单的时候,因订单的种类比较多,例如:采购订单、销售订单、调拨订单、预售订单等待,如果这些订单的差异比较大,需要分开建多个表,这样后台接口就需要设计一番了。
下面就按步骤直接贴代码案例
第一步: 创建一个接口,定义接收订单的方法;
public interface OrderRepeaterServiceI {
Result receiveOrder(ReceiveProductOrder order);
}
第二步: 定义接口的实现类,2个@Service
br/>@Service
br/>@Override
System.out.println("采购订单执行分支…");
return null;
}
@Service
public class SaleOrderServiceImpl implements OrderRepeaterServiceI {@Override
br/>@Override
System.out.println("销售订单执行分支…");
return null;
}
}
第三步: 定义分发器服务@Service
br/>@Service
@Resource
private OrderRepeaterServiceI purchaseOrderServiceImpl;
@Resource
private OrderRepeaterServiceI saleOrderServiceImpl;
public Result receiveOrder(String orderType, ReceiveProductOrder order) throws Exception {
OrderRepeaterServiceI orderService = null;
switch (orderType) {
case "10" : orderService = purchaseOrderServiceImpl;break;
case "20" : orderService = saleOrderServiceImpl;break;
}
if (orderService != null) {
orderService.receiveOrder(order);
} else {
throw new RuntimeException("订单类型未知异常.");
}
return Result.successWithData("成功");
}
}
第四步: 定义接收源系统的控制器@Controller
br/>@Controller
public class OrderApiController {
private static final Logger logger = Logger.getLogger(OrderSapApiController.class);
@Resource
private OrderRepeaterService orderRepeaterService;
@RequestMapping(value = "/testService")
@ResponseBody
public Result testService(String orderType) {
try {
ReceiveProductOrder order = new ReceiveProductOrder();
order.setOrderTypeCode("10");
order.setRelatedDocCode("SO18031610001");
return orderRepeaterService.receiveOrder(orderType, order);
} catch (Exception e) {
logger.error("接口测试异常.");
return Result.errorWithData("测试异常");
}
}
}
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/191991.html