wuxw
2019-05-09 e546f6e157e79e4074ca23f7e4e48a5c9aea4fdb
OrderService/src/main/java/com/java110/order/api/OrderApi.java
@@ -32,15 +32,20 @@
@RequestMapping(path = "/orderApi")
public class OrderApi extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(OrderApi.class);
    private static Logger logger = LoggerFactory.getLogger(OrderApi.class);
    @Autowired
    private IOrderServiceSMO orderServiceSMOImpl;
    @RequestMapping(path = "/service",method= RequestMethod.POST)
    @ApiOperation(value="中心服务订单受理", notes="test: 返回 200 表示服务受理成功,其他表示失败")
    @ApiImplicitParam(paramType="query", name = "orderInfo", value = "订单受理信息", required = true, dataType = "String")
    /**
     * 订单请求服务
     * @param orderInfo 订单信息
     * @param request 请求对象
     * @return ResponseEntity 对象
     */
    @RequestMapping(path = "/service", method = RequestMethod.POST)
    @ApiOperation(value = "中心服务订单受理", notes = "test: 返回 200 表示服务受理成功,其他表示失败")
    @ApiImplicitParam(paramType = "query", name = "orderInfo", value = "订单受理信息", required = true, dataType = "String")
    public ResponseEntity<String> servicePost(@RequestBody String orderInfo, HttpServletRequest request) {
        ResponseEntity<String> responseEntity = null;
@@ -48,62 +53,67 @@
        try {
            Map<String, String> headers = new HashMap<String, String>();
            getRequestInfo(request, headers);
            logger.debug("订单服务请求报文为: {},请求头为:{}",orderInfo,headers);
            logger.debug("订单服务请求报文为: {},请求头为:{}", orderInfo, headers);
            //接受请求事件
            DataFlowEventPublishing.receiveRequest(orderInfo,headers);
            DataFlowEventPublishing.receiveRequest(orderInfo, headers);
            //预校验
            preValiateOrderInfo(orderInfo);
            responseEntity =  orderServiceSMOImpl.service(orderInfo, headers);
        }catch (Exception e){
            logger.error("请求订单异常",e);
            responseEntity =  new ResponseEntity<String>("请求中心服务发生异常,"+e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }finally {
            responseEntity = orderServiceSMOImpl.service(orderInfo, headers);
        } catch (Exception e) {
            logger.error("请求订单异常", e);
            responseEntity = new ResponseEntity<String>("请求中心服务发生异常," + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        } finally {
            logger.debug("订单服务返回报文为: {}",responseEntity);
            logger.debug("订单服务返回报文为: {}", responseEntity);
            return responseEntity;
        }
    }
    /**
     * 这里预校验,请求报文中不能有 dataFlowId
     *
     * @param orderInfo
     */
    private void preValiateOrderInfo(String orderInfo) {
        Assert.jsonObjectHaveKey(orderInfo,"orders","请求报文中未包含orders节点,"+orderInfo);
        Assert.jsonObjectHaveKey(orderInfo, "orders", "请求报文中未包含orders节点," + orderInfo);
        Assert.jsonObjectHaveKey(orderInfo,"business","请求报文中未包含business节点,"+orderInfo);
        Assert.jsonObjectHaveKey(orderInfo, "business", "请求报文中未包含business节点," + orderInfo);
        if(JSONObject.parseObject(orderInfo).getJSONObject("orders").containsKey("dataFlowId")){
            throw new BusinessException(ResponseConstant.RESULT_CODE_ERROR,"报文中不能存在dataFlowId节点");
        if (JSONObject.parseObject(orderInfo).getJSONObject("orders").containsKey("dataFlowId")) {
            throw new BusinessException(ResponseConstant.RESULT_CODE_ERROR, "报文中不能存在dataFlowId节点");
        }
    }
    /**
     * 这里预校验,请求报文中不能有 dataFlowId
     *
     * @param orderInfo
     */
    private void preValiateOrderInfo(String orderInfo,Map<String, String> headers) {
    private void preValiateOrderInfo(String orderInfo, Map<String, String> headers) {
        Assert.hasKey(headers,"serviceCode","没有包含serviceCode");
        Assert.hasKey(headers, "serviceCode", "没有包含serviceCode");
        Assert.hasLength(headers.get("serviceCode"),"serviceCode 不能为空");
        Assert.hasLength(headers.get("serviceCode"), "serviceCode 不能为空");
        Assert.hasKey(headers,"appId","没有包含appId");
        Assert.hasKey(headers, "appId", "没有包含appId");
        Assert.hasLength(headers.get("appId"),"appId 不能为空");
        Assert.hasLength(headers.get("appId"), "appId 不能为空");
    }
    /**
     * 获取请求信息
     *
     * @param request
     * @param headers
     * @throws RuntimeException
     */
    private void getRequestInfo(HttpServletRequest request,Map headers) throws Exception{
        try{
            super.initHeadParam(request,headers);
            super.initUrlParam(request,headers);
        }catch (Exception e){
            logger.error("加载头信息失败",e);
    private void getRequestInfo(HttpServletRequest request, Map headers) throws Exception {
        try {
            super.initHeadParam(request, headers);
            super.initUrlParam(request, headers);
        } catch (Exception e) {
            logger.error("加载头信息失败", e);
            throw e;
        }
    }