1.导入maven依赖
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.35</version>
</dependency>
2.测试代码
package com.example.demo05.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo05.entity.list_v_IotBox;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.*;
@RestController
@RequestMapping("/api/demo01")
public class demo01 {
@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public JSONObject demo() {
HashMap<String, String> stringStringHashMap = new HashMap<>();
stringStringHashMap.put("iDisplayStart","0");
stringStringHashMap.put("iDisplayLength","10");
String s = doPost("http://localhost:8988/bmpl/api/iot/get_v_iot_box", null, stringStringHashMap);
JSONObject jsonObject = JSONObject.parseObject(s);
JSONArray v_iot_box = jsonObject.getJSONArray("v_iot_box");
for (Object vIotBox : v_iot_box) {
//object 转 jsonObject
list_v_IotBox vIotBox1 = JSONObject.toJavaObject((JSON)vIotBox,list_v_IotBox.class);
System.out.println("------------>"+vIotBox1);
}
// System.out.println("请求返回值:"+s);
return jsonObject;
}
public static String doPost(String url, Map<String, Object> headers, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
HttpPost httpPost = new HttpPost(url);
if (headers != null) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
httpPost.addHeader(entry.getKey(), entry.getValue());
}
}
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
}