记录我自己的工作

get 请求  ,携带 请求头 header (token)

url 根据参数 动态拼接

参数   放入 map  动态拼接

  1. private String lclUrl = "http://xxx.xxxx.com/lcl";

  

  1. private String TOKEN360FOB_URL = "http://xxx.xxxxxx.com/token?username={name}&password={password}";

  

  1. public JSONObject get360fobToken() {
  2. String json = restTemplate.getForObject(TOKEN360FOB_URL, String.class, username, password);
  3. JSONObject json1 = JSONObject.fromObject(json);
  4. return json1;
  5. }
  1. /**
  2. * 拼箱
  3. *
  4. * @param departure
  5. * @param departureDate
  6. * @param destination
  7. * @param destCountry
  8. * @param lsps
  9. * @param sortBy
  10. * @param pageNum
  11. * @param pageSize
  12. * @return
  13. */
  14. public JSONObject getLclFreight(String departure, String departureDate, String destination, String destCountry, String lsps, String sortBy, int pageNum, int pageSize) {
  15. return this.getFclAndLcl(departure, departureDate, destination,
  16. destCountry, lsps, sortBy, pageNum, pageSize, lclUrl);
  17. }
  1. /**
  2. * 共同代码抽取
  3. *
  4. * @param departure
  5. * @param departureDate
  6. * @param destination
  7. * @param destCountry
  8. * @param lsps
  9. * @param sortBy
  10. * @param pageNum
  11. * @param pageSize
  12. * @param xxxUrl
  13. * @return
  14. */
  15. private JSONObject getFclAndLcl(String departure, String departureDate, String destination,
  16. String destCountry, String lsps, String sortBy, int pageNum, int pageSize, String xxxUrl) {
  17. JSONObject json = this.get360fobToken();
  18. String t = json.getString("content");
  19. String token = "Bearer " + t;
           // 将token 放入请求头
  20. HttpHeaders requestHeaders = new HttpHeaders();
  21. requestHeaders.add("Authorization", token);
  22.  
  23. //参数
  24. Map<String, Object> uriVariables = new HashMap<String, Object>();
  25. //拼接url
  26. StringBuffer buffer = new StringBuffer();
  27. buffer.append("?");
  28. if (StringUtils.isNotBlank(departure)) {
  29. buffer.append("departure={departure}").append("&");
  30. uriVariables.put("departure", departure);
  31. }
  32. if (StringUtils.isNotBlank(departureDate)) {
  33. buffer.append("departureDate={departureDate}").append("&");
  34. uriVariables.put("departureDate", departureDate);
  35. }
  36. if (StringUtils.isNotBlank(destination)) {
  37. buffer.append("destination={destination}").append("&");
  38. uriVariables.put("destination", destination);
  39. }
  40. if (StringUtils.isNotBlank(destCountry)) {
  41. buffer.append("destCountry={destCountry}").append("&");
  42. uriVariables.put("destCountry", destCountry);
  43. }
  44. if (StringUtils.isNotBlank(lsps)) {
  45. buffer.append("lsps={lsps}").append("&");
  46. uriVariables.put("lsps", lsps);
  47. }
  48. if (StringUtils.isNotBlank(sortBy)) {
  49. buffer.append("sortBy={sortBy}").append("&");
  50. uriVariables.put("sortBy", sortBy);
  51. }
  52.  
  53. buffer.append("pageNum={pageNum}").append("&");
  54. buffer.append("pageSize={pageSize}");
  55. uriVariables.put("pageNum", pageNum);
  56. uriVariables.put("pageSize", pageSize);
  57. String url = xxxUrl + buffer.toString();
  58.  
  59. HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
  60.  
  61. ResponseEntity<String> response =
  62. restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class, uriVariables);
  63. String resBody = response.getBody();
  64. JSONObject temp = JSONObject.fromObject(resBody);
  65.  
  66. return temp;
  67. }

工作疑问:

可以用更简洁的方法 拼接 url 吗

个人qq : 332893400

AngDH.Lee

用RestTemplate调取接口,取得返回数据,携带header,动态拼接url ,动态参数的更多相关文章

  1. charles_01_打断点修改接口请求&返回数据

    前言 测试过程中,为了模拟某场景测试,经常需要修改接口请求或者返回数据.可通过抓包工具打断点,实现模拟测试场景.常用的抓包工具charles和fiddler均可打断点mock数据.由于小编安装了cha ...

  2. Requests方法 -- 参数关联与JSESSION(上一个接口的返回数据作为下一个接口的请求参数)

    前言 参数关联是接口测试和性能测试最为重要的一个步骤,很多接口的请求参数是动态的,并且需要从上一个接口的返回值里面取出来,一般只能用一次就失效了.最常见的案例就是网站的登录案例,很多网站的登录并不仅仅 ...

  3. Java封装接口统一返回数据模板

    现在大多数都使用前后端分离开发模式,前端通过Ajax请求访问后台服务器,后台返回JSON数据供前端操作,这里编写一个统一返回数据模板类,方便日后操作 public class R extends Ha ...

  4. PHP开发APP接口之返回数据

    首先说明一下客户端APP通信的格式 1.xml:扩展标记语言(1.用来标记数据,定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言,xml格式统一,跨平台和语言,非常适合数据传输和通信,早已 ...

  5. C#调用sap接口及返回数据到sap

    public class SapClass { /// <summary> /// /// </summary> /// <param name="fphm&q ...

  6. 远程带参数POST访问接口,返回数据

    1. string token = GetRequest.GetString("token"); int customer_id = GetRequest.GetInt(" ...

  7. 关于 ajax 动态返回数据 css 以及 js 失效问题(动态引入JS)

    ajax 毕竟是异步的 所以动态加载出来的数据 难免遇到 css 或者 js 失效的问题,所以要动态加载 css ji等文件了 1.公共方法 load //动态加载 js /css function ...

  8. python3乱码问题:接口返回数据中文乱码问题解决

    昨天测试接口出现有一个接口中文乱码问题,现象: 1 浏览器请求返回显示正常 2 用代码请求接口返回数据中文显示乱码 3 使用的python3,python3默认unicode编码,中文都是可以正常显示 ...

  9. java 接口自动化测试之数据请求的简单封装

    我们自己用java写接口自动化测试框架或者做个接口自动化测试平台的话,是需要自己进行相关的请求的,因此我们需要简单的封装下httpclient,我新建了一个http工具类,将get方法和post方法进 ...

随机推荐

  1. 01_Mac下安装homebrew

    参考:https://jingyan.baidu.com/album/fec7a1e5ec30341190b4e7e5.html?picindex=3 1.在打开的命令行工具中输入如下语句: ruby ...

  2. Redhat Linux 7.3 虚拟机通过USB挂载NTFS格式的移动硬盘

    分为如下几个步骤: 一.设置本地yum,安装gcc(如果本机已经安装gcc,则跳过此步) 在虚拟机连接linux iso安装盘 查看光盘挂载情况 mkdir /iso mount /dev/cdrom ...

  3. mac apache服务器

    //开启apache: sudo apachectl start //重启apache: sudo apachectl restart //关闭apache: sudo apachectl stop ...

  4. SQL Server ->> 尝试优化ETL中优化Merge性能

    这几天突发想到在ETL中Merge性能的问题.思路的出发点是Merge到目标表需要扫描的数据太多,而现实情况下,假设应该是只有一小部分会被更新,而且这部分数据也应该是比较新的数据,比方说对于想Fact ...

  5. 代码分析工具推荐Understand

    之前看ogitor ,第一次看到那么多代码~~不知道从哪里下手,而且好多东西都不会Ogre什么的都不是很清楚,对ogitor的代码结构的了解就更不用提了.晕头转向的 不知道从哪里下手,一点点的看起来好 ...

  6. python 中的list&tuple

    list Python内置的一种数据类型是列表:list>>> classmates = ['Michael', 'Bob', 'Tracy']>>> classm ...

  7. July 06th 2017 Week 27th Thursday

    Knowledge is the antidote to fear. 知识可以解除恐惧. Fear always steps from unknown things. Once we know wha ...

  8. 最详细的Vue Hello World应用开发步骤

    很多Vue的初学者想尝试这个框架时,都被webpack过于复杂的配置所吓倒,导致最后无法跑出一个期望的hello word效果.今天我就把我第一次使用webpack打包一个Vue Hello Worl ...

  9. 利用Underscore求数组的交集、并集和差集

    1 数组交集函数——intersection 数组的交集是指包含多个数组中的共同元素的一个数组,求数组的交集就是找出给定数组中的共有元素. 下面实现一个求两个数组交集的函数. 判断数组是够包含指定值, ...

  10. hdu-2886 Special Prime---数论推导

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2866 题目大意: 问你1到L中有多少个素数满足n^3 + p*n^2 = m^3(其中n,m为大于1 ...