寫(xiě)代碼之前你必須要搞清楚這幾件事
- 數(shù)據(jù)流轉(zhuǎn)方向(是己方主動(dòng)請(qǐng)求第三方接口還是第三方推送數(shù)據(jù))
- 數(shù)據(jù)對(duì)接的方式(http網(wǎng)絡(luò)傳輸/MySql/sqlServer/ORACLE/WebService)
- 數(shù)據(jù)傳輸報(bào)文格式(json/xml)
1. 第三方數(shù)據(jù)推送的情況
一般如果是第三方主動(dòng)推送數(shù)據(jù)的話(huà),對(duì)方會(huì)提供相應(yīng)的文檔,重點(diǎn)查看接口地址以及傳輸過(guò)來(lái)的數(shù)據(jù)格式,以及對(duì)返回格式有無(wú)要求。此方式一般為http請(qǐng)求。
示例代碼:
@ResponseBody
@RequestMapping(value = "parking/upParkingSpace", method = RequestMethod.POST)
public ResultMsg upParkingSpace(HttpServletRequest request) throws IOException {
String data = IOUtils.toString(request.getInputStream(), "UTF-8");
logger.warn(data);
//關(guān)流
reader.close();
//解析
DrfCount drfCount = JSONObject.parseObject(data,DrfCount.class);
//具體業(yè)務(wù)邏輯
···
ResultMsg resultMsg = new ResultMsg();
resultMsg.setState("1");
resultMsg.setMsg("操作成功");
return resultMsg;
}
使用到的插件:
- 引入 maven
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.29</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
- json轉(zhuǎn)對(duì)象在線轉(zhuǎn)換
- postman 測(cè)試接口地址。