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; }
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;
}
浙公网安备 33010602011771号