Interface design for similar services with multiple branches
Interface design of multi-branches of the same business
Scene description
For example, in the downstream system, when receiving planned orders from the source system, there are many kinds of orders, such as purchase orders, sales orders, transfer orders, pre-sale orders, etc., if the differences between these orders are relatively large, multiple tables need to be built separately, so the background interface needs to be designed.
Next, follow the steps to directly paste the code case.
Step 1: create an interface that defines how to receive orders
Public interface OrderRepeaterServiceI {
Result receiveOrder (ReceiveProductOrder order)
}
Step 2: define the implementation class of the interface, 2 @ Servicebr/ > @ Servicebr/ > @ Override
System.out.println ("purchase order execution Branch.")
Return null
}
@ Service
Public class SaleOrderServiceImpl implements OrderRepeaterServiceI {@ Overridebr/ > @ Override
System.out.println ("sales order execution Branch.")
Return null
}
}
Step 3: define the distributor service @ Servicebr/ > @ Service
@ Resourceprivate OrderRepeaterServiceI purchaseOrderServiceImpl;@Resourceprivate 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 ("order type unknown exception.") } return Result.successWithData ("success");}
}
Step 4: define the controller @ Controllerbr/ > @ Controller of the receiving source system
Public class OrderApiController {
Private static final Logger logger = Logger.getLogger (OrderSapApiController.class); @ Resourceprivate OrderRepeaterService orderRepeaterService;@RequestMapping (value = "/ testService") @ ResponseBodypublic 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 ("Interface Test exception.") Return Result.errorWithData (Test exception);}}
}