|
| 1 | +package com.java110.comment.api; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSONObject; |
| 4 | +import com.java110.comment.smo.ICommentServiceSMO; |
| 5 | +import com.java110.common.constant.ResponseConstant; |
| 6 | +import com.java110.common.exception.InitConfigDataException; |
| 7 | +import com.java110.common.exception.InitDataFlowContextException; |
| 8 | +import com.java110.core.base.controller.BaseController; |
| 9 | +import com.java110.core.context.BusinessServiceDataFlow; |
| 10 | +import com.java110.core.factory.DataTransactionFactory; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.web.bind.annotation.RequestBody; |
| 13 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 14 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 15 | +import org.springframework.web.bind.annotation.RestController; |
| 16 | + |
| 17 | +import javax.servlet.http.HttpServletRequest; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +/** |
| 22 | + * 用户服务类 |
| 23 | + * Created by wuxw on 2018/5/14. |
| 24 | + */ |
| 25 | +@RestController |
| 26 | +public class CommentApi extends BaseController { |
| 27 | + |
| 28 | + @Autowired |
| 29 | + ICommentServiceSMO commentServiceSMOImpl; |
| 30 | + |
| 31 | + @RequestMapping(path = "/commentApi/service",method= RequestMethod.GET) |
| 32 | + public String serviceGet(HttpServletRequest request) { |
| 33 | + return DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_ERROR,"不支持Get方法请求").toJSONString(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * 用户服务统一处理接口 |
| 38 | + * @param orderInfo |
| 39 | + * @param request |
| 40 | + * @return |
| 41 | + */ |
| 42 | + @RequestMapping(path = "/commentApi/service",method= RequestMethod.POST) |
| 43 | + public String servicePost(@RequestBody String orderInfo, HttpServletRequest request) { |
| 44 | + BusinessServiceDataFlow businessServiceDataFlow = null; |
| 45 | + JSONObject responseJson = null; |
| 46 | + try { |
| 47 | + Map<String, String> headers = new HashMap<String, String>(); |
| 48 | + getRequestInfo(request, headers); |
| 49 | + //预校验 |
| 50 | + preValiateOrderInfo(orderInfo); |
| 51 | + businessServiceDataFlow = this.writeDataToDataFlowContext(orderInfo, headers); |
| 52 | + responseJson = commentServiceSMOImpl.service(businessServiceDataFlow); |
| 53 | + }catch (InitDataFlowContextException e){ |
| 54 | + logger.error("请求报文错误,初始化 BusinessServiceDataFlow失败"+orderInfo,e); |
| 55 | + responseJson = DataTransactionFactory.createNoBusinessTypeBusinessResponseJson(orderInfo,ResponseConstant.RESULT_PARAM_ERROR,e.getMessage(),null); |
| 56 | + }catch (InitConfigDataException e){ |
| 57 | + logger.error("请求报文错误,加载配置信息失败"+orderInfo,e); |
| 58 | + responseJson = DataTransactionFactory.createNoBusinessTypeBusinessResponseJson(orderInfo,ResponseConstant.RESULT_PARAM_ERROR,e.getMessage(),null); |
| 59 | + }catch (Exception e){ |
| 60 | + logger.error("请求订单异常",e); |
| 61 | + responseJson = DataTransactionFactory.createBusinessResponseJson(businessServiceDataFlow,ResponseConstant.RESULT_CODE_ERROR,e.getMessage()+e, |
| 62 | + null); |
| 63 | + }finally { |
| 64 | + return responseJson.toJSONString(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * 这里预校验,请求报文中不能有 dataFlowId |
| 70 | + * @param orderInfo |
| 71 | + */ |
| 72 | + private void preValiateOrderInfo(String orderInfo) { |
| 73 | + /* if(JSONObject.parseObject(orderInfo).getJSONObject("orders").containsKey("dataFlowId")){ |
| 74 | + throw new BusinessException(ResponseConstant.RESULT_CODE_ERROR,"报文中不能存在dataFlowId节点"); |
| 75 | + }*/ |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * 获取请求信息 |
| 80 | + * @param request |
| 81 | + * @param headers |
| 82 | + * @throws RuntimeException |
| 83 | + */ |
| 84 | + private void getRequestInfo(HttpServletRequest request,Map headers) throws Exception{ |
| 85 | + try{ |
| 86 | + super.initHeadParam(request,headers); |
| 87 | + super.initUrlParam(request,headers); |
| 88 | + }catch (Exception e){ |
| 89 | + logger.error("加载头信息失败",e); |
| 90 | + throw new InitConfigDataException(ResponseConstant.RESULT_PARAM_ERROR,"加载头信息失败"); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public ICommentServiceSMO getCommentServiceSMOImpl() { |
| 95 | + return commentServiceSMOImpl; |
| 96 | + } |
| 97 | + |
| 98 | + public void setCommentServiceSMOImpl(ICommentServiceSMO commentServiceSMOImpl) { |
| 99 | + this.commentServiceSMOImpl = commentServiceSMOImpl; |
| 100 | + } |
| 101 | +} |
0 commit comments