最近项目中有用到与外围系统对接, 总结下通过java.net.URLConnection发送HTTP请求的方法。
参考博客:http://www.cnblogs.com/nick-huang/p/3859353.html
由于项目较小,并且只查询一个外部接口数据,采用了简单的get请求;
完整代码如下:
1 package com.ruoyi.api; 2 3 import javax.servlet.http.HttpServletRequest; 4 5 import com.alibaba.fastjson.JSON; 6 import com.alibaba.fastjson.JSONObject; 7 import com.ruoyi.api.bus.vo.LinksV2; 8 import com.ruoyi.api.bus.vo.TopicItemListV2; 9 import org.apache.commons.collections.CollectionUtils; 10 import org.slf4j.Logger; 11 import org.slf4j.LoggerFactory; 12 import org.springframework.beans.factory.annotation.Value; 13 import org.springframework.stereotype.Controller; 14 import org.springframework.web.bind.annotation.RequestMapping; 15 import org.springframework.web.bind.annotation.ResponseBody; 16 import java.io.BufferedReader; 17 import java.io.IOException; 18 import java.io.InputStreamReader; 19 import java.net.MalformedURLException; 20 import java.net.URL; 21 import java.net.URLConnection; 22 23 import java.util.ArrayList; 24 import java.util.List; 25 26 @Controller 27 @RequestMapping("api") 28 public class testcontroller { 29 30 @Value("${config.getItemInfoUrl}") 31 private String url; 32 private static final Logger LOGGER = LoggerFactory.getLogger(testcontroller.class); 33 34 @ResponseBody 35 @RequestMapping("/data/getlist") 36 public List<Links> testlist(HttpServletRequest request) { 37 String jsonInfo = loadJSON(url); 38 List<TopicItemListV2> topicItemListV2s = JSONObject.parseArray(jsonInfo, TopicItemListV2.class); 39 List<Links> linkLists = new ArrayList<>(); 40 if (CollectionUtils.isNotEmpty(topicItemListV2s)){ 41 for (TopicItemListV2 topicItemList : topicItemListV2s){ 42 Links links = new Links(); 43 links.setPid("0"); 44 links.setCode(String.valueOf(topicItemList.getCode())); 45 links.setName(topicItemList.getName()); 46 linkLists.add(links); 47 List<LinksV2> linkV2sss = topicItemList.getLinks(); 48 if (CollectionUtils.isNotEmpty(linkV2sss)){ 49 for (LinksV2 li :linkV2sss){ 50 Links linkvvv = new Links(); 51 linkvvv.setCode(li.getTaskCode()); 52 linkvvv.setName(li.getTaskName()); 53 linkvvv.setPid(String.valueOf(topicItemList.getCode())); 54 linkLists.add(linkvvv); 55 } 56 } 57 } 58 } 59 LOGGER.info(JSON.toJSONString(linkLists)); 60 return linkLists; 61 } 62 63 public String loadJSON(String url) { 64 StringBuilder json = new StringBuilder(); 65 BufferedReader in = null; 66 try { 67 // 创建url对象 68 URL reqUrl = new URL(url); 69 // 打开url链接 70 URLConnection yc = reqUrl.openConnection(); 71 // 建立实际对象 72 yc.connect(); 73 // 定义BufferedReader 数据流来读取URL的相应,并设置编码方式 74 in = new BufferedReader(new InputStreamReader( 75 yc.getInputStream(),"utf-8")); 76 String inputLine = null; 77 //读取内容 78 while ((inputLine = in.readLine()) != null) { 79 json.append(inputLine); 80 } 81 } catch (MalformedURLException e) { 82 LOGGER.error(" error info" + e); 83 } catch (IOException e) { 84 LOGGER.error(" error info" + e); 85 } finally { 86 if (null != in){ 87 try { 88 in.close(); 89 } catch (IOException e) { 90 e.printStackTrace(); 91 } 92 } 93 } 94 return json.toString(); 95 } 96 }