核心包:

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

一:接收数据

json数据格式如下:

1、单一对象型:

a、数据格式

b、接口java(核心)代码范例

    @ResponseBody
@RequestMapping(value = "/specialtyOA2DMS.do", method = RequestMethod.POST)
public JSONObject specialtyOA2DMS(@RequestBody JSONObject jsonObject, HttpServletRequest request)
throws GenericBusinessException {
JSONObject return_json = new JSONObject(); try {
//校验入参
if (StringTool.isEmpty(jsonObject.toString())) {
return_json.put("RESULT_STATUS", "F");
return_json.put("RESULT_REASON", "入参错误"); return return_json;
}
//校验入参单头不能为空
if(StringTool.isEmpty(jsonObject.getString("APPLY_DATE")) || StringTool.isEmpty(jsonObject.getString("OA_FORM_NO")) || StringTool.isEmpty(jsonObject.getString("SPECIALTY_LINE"))){
return_json.put("RESULT_STATUS", "F");
return_json.put("RESULT_REASON", "入参必填项存在空值(OA_FORM_NO-OA流程单号;APPLY_DATE-申请日期)"); return return_json;
} else {
String oaFormNoc = (String)jsonObject.getString("OA_FORM_NO");//OA流程单号 //校验入参单身数据
JSONArray specialtyLine = jsonObject.getJSONArray("SPECIALTY_LINE");
if(null != specialtyLine && specialtyLine.size()>0){
for(Object object : specialtyLine){
JSONObject obj = (JSONObject)object;
String sapContractNo = (String)obj.getString("SAP_CONTRACT_NO");//SAP合同编号
String prodCode = (String)obj.getString("SPECIALTY_PROD_CODE");//特制品编码
String unit = (String)obj.getString("SPECIALTY_PROD_UNIT");//单位
String qty = (String)obj.getString("SPECIALTY_PROD_QTY");//数量
//必填不能为空
if(StringTool.isEmpty(sapContractNo) || StringTool.isEmpty(prodCode) || StringTool.isEmpty(unit) || StringTool.isEmpty(qty)){
return_json.put("RESULT_STATUS", "F");
return_json.put("RESULT_REASON", "入参必填项存在空值(SAP_CONTRACT_NO-SAP合同编号/SPECIALTY_PROD_CODE-特制品编码/SPECIALTY_PROD_UNIT-单位/SPECIALTY_PROD_QTY-申请数量)"); return return_json;
} }
return_json.put("RESULT_STATUS", "S");
return_json.put("RESULT_REASON", "成功");
return return_json;
} else {
return_json.put("RESULT_STATUS", "F");
return_json.put("RESULT_REASON", "入参错误"); return return_json;
}
} } catch(Exception e){
return_json.put("RESULT_STATUS", "F");
return_json.put("RESULT_REASON", "入参错误");
return return_json;
} return return_json;
}

2、对象对象数组型:

a、数据格式

b、接口java(核心)代码范例

    @ResponseBody
@RequestMapping(value = "/specialtySAP2DMS.do", method = RequestMethod.POST)
public JSONObject specialtySAP2DMS(@RequestBody JSONObject[] jsonObject, HttpServletRequest request)
throws GenericBusinessException {
JSONObject return_json = new JSONObject();
SapImportData sapData = new SapImportData();
List<SpecialtyProduct> specialtyProductList = new ArrayList<SpecialtyProduct>(); try {
//校验入参
if(null != jsonObject && (jsonObject.length)>0){
for(JSONObject jsonObj : jsonObject){
SpecialtyProduct specialtyProduct = new SpecialtyProduct();
String prodCode = "";//物料编号
String prodDesc = "";//物料描述
String prodFlag = "";//标识
String prodRemark = "";//说明
String prodIscontr = "";//是否进行数量管控
if(StringTool.isEmpty(jsonObj.getString("PROD_CODE"))){
return_json.put("RESULT_STATUS", "F");
return_json.put("RESULT_REASON", "入参必填项存在空值(PROD_CODE-物料编号)");
this.sapLog(sapData, "", "参数为空", "入参必填项存在空值(PROD_CODE-物料编号)","specialtySAP2DMS.do");
return return_json;
} else {
prodCode = (String)jsonObj.getString("PROD_CODE");//物料编号
if(StringTool.isNotEmpty(jsonObj.getString("PROD_DESC"))){
prodDesc = (String)jsonObj.getString("PROD_DESC");
}
if(StringTool.isNotEmpty(jsonObj.getString("PROD_FLAG"))){
prodFlag = (String)jsonObj.getString("PROD_FLAG");
}
if(StringTool.isNotEmpty(jsonObj.getString("PROD_REMARK"))){
prodRemark = (String)jsonObj.getString("PROD_REMARK");
}
if(StringTool.isNotEmpty(jsonObj.getString("PROD_ISCONTR"))){
prodIscontr = (String)jsonObj.getString("PROD_ISCONTR");
}
specialtyProduct.setProdCode(prodCode);
specialtyProduct.setProdDesc(prodDesc);
specialtyProduct.setProdFlag(prodFlag);
specialtyProduct.setProdRemark(prodRemark);
specialtyProduct.setProdIscontr(prodIscontr);
//处理开始时间和结束时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
Calendar c = Calendar.getInstance();
Timestamp ct = new Timestamp(c.getTimeInMillis());//创建时间
specialtyProduct.setCreatedDate(ct); //通用字段
specialtyProduct.setCreatorId(1l);
specialtyProduct.setCreatorName("admin");
specialtyProduct.setModiDate(ct);
specialtyProduct.setModifierId(1l);
specialtyProduct.setModifierName("admin");
specialtyProduct.setDataSource("SAP汇入");
specialtyProduct.setOwnerId(1l);
//添加到List集合中批量保存
specialtyProductList.add(specialtyProduct);
}
} return_json.put("RESULT_STATUS", "S"); return_json.put("RESULT_REASON", "成功");
return return_json; } else {
return_json.put("RESULT_STATUS", "F");
return_json.put("RESULT_REASON", "入参错误"); return return_json;
} } catch(Exception e){
return_json.put("RESULT_STATUS", "F"); return_json.put("RESULT_REASON", "接口处理错误");
return return_json;
} }

二:传输数据到接口

a、数据格式

b、接口java(核心)代码范例

    @SuppressWarnings("unchecked")
@Override
public String AIReturnHttpPost(List<Object> ObjectList)throws GenericBusinessException{
String resultStr = "";
String entityJson = JSON.toJSONString(ObjectList);
String url = "";//填自己要调接口的路径
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
StringEntity entity = new StringEntity(entityJson, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httppost.setEntity(entity);
Character statusWL = new Character('F');
String message = ""; try {
HttpResponse result = client.execute(httppost);
String resData = EntityUtils.toString(result.getEntity(), "utf-8");
if(StringTool.isNotEmpty(resData)){
JSONObject jobg = JSON.parseObject(resData);
if(StringTool.isNotEmpty(jobg.get("code"))){
String code = jobg.get("code").toString(); if(StringTool.isNotEmpty(jobg.get("message"))){
message = jobg.get("message").toString();
}
if("200".equals(code)){
statusWL = new Character('T');
} else {
statusWL = new Character('F');
}
resultStr = resultStr+code+"-"+message;
}
} } catch (Exception e) {
message = "处理接口异常";
e.printStackTrace();
} return resultStr;
}

最新文章

  1. 订餐系统之微信支付,踩了官方demo的坑
  2. C#的互操作性:缓冲区、结构、指针
  3. Xcode 7如何免费真机调试iOS应用
  4. 主DNS配置
  5. 几种web数据渲染模板对比
  6. IBInspectable / IBDesignable
  7. (转载)postgresql navicat 客户端连接验证失败解决方法:password authentication failed for user
  8. VmodCAM 初始化
  9. 关于JSP异常的处理
  10. haskell学习笔记_函数
  11. Kafka的可靠性问题
  12. 框架搭建资源 (一) V(视图)C(控制)模式
  13. unity Editor的使用
  14. Pycharm快捷键的使用
  15. [BZOJ2326] [HNOI2011] 数学作业 (矩阵乘法)
  16. golang sync/atomic
  17. [Swift]LeetCode730. 统计不同回文子字符串 | Count Different Palindromic Subsequences
  18. Windows已遇到关键问题,将在一分钟后自动重新启动,请立即保存工作
  19. Excel 恢复默认行高、列宽
  20. Java第05次实验提纲(Java图形界面编程)

热门文章

  1. F - Dragon Balls
  2. Java2年开发工作经验面试总结
  3. redis:安装及基础知识(一)
  4. 【一起学设计模式】观察者模式实战:真实项目中屡试不爽的瓜娃EventBus到底如何实现观察者模式的?
  5. DDOS攻击攻击种类和原理
  6. 同步类的基础AbstractQueuedSynchronizer(AQS)
  7. 痞子衡嵌入式:揭秘i.MXRT1170 eFuse空间访问可靠性的保护策略(冗余与ECC)
  8. MYSQL隔离级别 与 锁
  9. PAT A1023
  10. 基于Swoole的HTTP/HTTPS代理