HttpClient之Post介面程式碼範例
核心包:
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; }
View Code
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; } }
View Code
二:傳輸數據到介面
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; }
View Code