记录下 自己用的Java用后台发http请求并返回数据的方式

首先在pom.xml中引入

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.56</version>
</dependency>



import
org.apache.http.HttpEntity; import org.apache.http.ParseException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; /** * * @throws Exception */ @ResponseBody @RequestMapping(value="getWorkFlowTempletFilter") public List<Map<String, Object>> getWorkFlowTempletFilter(@RequestParam("filter") String filter) throws Exception{ String url="http://XX.XX.XX.XX:XX/workflow/getWorkFlowTempletFilter"; if(filter !=null){ url +="?filter=" + filter; } System.out.println(url); //1.获得一个httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); //2.生成一个get请求 HttpGet httpget = new HttpGet(url); CloseableHttpResponse response = null; try { //3.执行get请求并返回结果 response = httpclient.execute(httpget); } catch (IOException e1) { e1.printStackTrace(); } String result = null; try { //4.处理结果,这里将结果返回为字符串 HttpEntity entity = response.getEntity(); if (entity != null) { result = EntityUtils.toString(entity); } } catch (ParseException | IOException e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>(); JSONArray objectAttr = JSONArray.parseArray(result); Map<String, Object> retMap = new HashMap<String, Object>(); if(objectAttr.size()>0){ for(int i=0;i<objectAttr.size();i++){ JSONObject jsonObject = objectAttr.getJSONObject(i); retMap = toMap(jsonObject); listMap.add(retMap); } } return listMap; } public static Map<String, Object> toMap(JSONObject object) throws JSONException { Map<String, Object> map = new HashMap<String, Object>(); Set<String> keysItr = object.keySet(); Iterator<String> it = keysItr.iterator(); while(it.hasNext()) { String key = it.next(); Object value = object.get(key); map.put(key, value); } return map; } /* public static Map<String, Object> toMap(JSONObject object) throws JSONException { Map<String, Object> map = new HashMap<String, Object>(); Iterator<String> keysItr = (Iterator<String>) object.keySet(); while(keysItr.hasNext()) { String key = keysItr.next(); Object value = object.get(key); if(value instanceof JSONArray) { value = toList((JSONArray) value); } else if(value instanceof JSONObject) { value = toMap((JSONObject) value); } map.put(key, value); } return map; } public static List<Object> toList(JSONArray array) throws JSONException { List<Object> list = new ArrayList<Object>(); for(int i = 0; i < array.size(); i++) { Object value = array.get(i); if(value instanceof JSONArray) { value = toList((JSONArray) value); } else if(value instanceof JSONObject) { value = toMap((JSONObject) value); } list.add(value); } return list; } */
原文地址:https://www.cnblogs.com/Samuel-Leung/p/11150885.html