写代码重要,写好的代码准确无误,且符合预期那更是必不可少。

spring boot内嵌了专门的单元测试模块——RestTemplate,保证了程序员可以对自己的代码进行及时的测试。

闲言少叙,直接上代码吧,简单的get/post方法都可以在这里测试,避免了自己写JDK原生的URLConnection、或Apache的Http Client方法。

一、什么是RestTemplate?

RestTemplate是Spring提供的用于访问Rest服务的客户端。

RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。

调用RestTemplate的默认构造函数,RestTemplate对象在底层通过使用java.net包下的实现创建HTTP 请求。

二、常用的方法:

1.getForEntity方法的返回值是一个ResponseEntity<T>ResponseEntity<T>是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等。

2.getForObject函数实际上是对getForEntity函数的进一步封装,如果你只关注返回的消息体的内容,对其他信息都不关注,此时可以使用getForObject

3.postForEntity 和getForEntity类似

4.postForObject 如果只关注,返回的消息体,可以直接使用postForObject。用法和getForObject一致

5.postForLocation也是提交新资源,提交成功之后,返回新资源的URI,postForLocation的参数和前面两种的参数一致,只不过返回值为Uri,只需要服务提供者返回一个Uri即可,该Uri表示新资源的位置。

6.其他的还有put(),delete()都不怎么常用

三、代码

有一个特别的地方,post如果传参数只能用LinkedMultiValueMap

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class UrlOnlineTests {
private RestTemplate template =new RestTemplate();
@Test
public void testGet(){
try {
String url = "http://localhost:8080/selectSmallVideo?sdt=20180531&edt=20180531";
String result = template.getForObject(url, String.class);
System.err.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
/**为什么这个地方只能用LinkedMutiValueMap*/
@Test
public void testPost(){
try {
String url = "http://localhost:8080/selectSmallVideo2";
LinkedMultiValueMap<String, Integer> map = new LinkedMultiValueMap<>();
map.add("sdt", 20180531);
map.add("edt", 20180531);
String result = template.postForObject(url,map, String.class);
System.err.println(result);
} catch (Exception e) {
e.printStackTrace();
}
} @Test
public void testGet2(){
try {
String url = "http://localhost:8080/selectSmallVideo?sdt=20180531&edt=20180531";
ResponseEntity<String> entity = template.getForEntity(url, String.class);
HttpStatus code = entity.getStatusCode();
System.err.println(code);
System.err.println(entity.toString());
} catch (Exception e) {
e.printStackTrace();
}
} @Test
public void testExchange(){
try {
String url = "http://localhost:8080/selectSmallVideo?sdt=20180531&edt=20180531";
HttpHeaders headers=new HttpHeaders();
headers.add("devid","");
headers.add("appver","9.3");
HttpEntity<String> entity = new HttpEntity<>(null,headers);
ResponseEntity<String> exchange = template.exchange(url, HttpMethod.GET, entity, String.class);
System.err.println(exchange.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}

spring boot单元测试之RestTemplate(一)的更多相关文章

  1. spring boot单元测试之RestTemplate(二)

    上篇博客中,简单介绍了RestTemplate,只是用到了单元测试环节,如果在正式开发环境使用RestTemplate调用远程接口,还有一些配置要做. 一.配置类 由于Spring boot没有对Re ...

  2. spring boot单元测试之RestTemplate(三)——api详解

    本篇内容来自翟永超的<Springcloud微服务实战>,转载请注明. 一.GET请求 在RestTemplate中,对GET请求可以通过如下两个方法进行调用实现. 第一种:getForE ...

  3. spring boot单元测试之MockMvc

    spring单元测试之MockMvc,这个只是模拟,并不是真正的servlet,所以session.servletContext是没法用的. @RunWith(SpringRunner.class) ...

  4. spring boot项目配置RestTemplate超时时长

    配置类: @Configuration public class FeignConfiguration { @Bean(name="remoteRestTemplate") pub ...

  5. Spring Boot(三):RestTemplate提交表单数据的三种方法

    http://blog.csdn.net/yiifaa/article/details/77939282 ********************************************** ...

  6. Spring Boot 16 条最佳实践

    Spring Boot是最流行的用于开发微服务的Java框架.在本文中,我将与你分享自2016年以来我在专业开发中使用Spring Boot所采用的最佳实践.这些内容是基于我的个人经验和一些熟知的Sp ...

  7. 玩转单元测试之Testing Spring MVC Controllers

    玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...

  8. spring boot / cloud (八) 使用RestTemplate来构建远程调用服务

    spring boot / cloud (八) 使用RestTemplate来构建远程调用服务 前言 上周因家里突发急事,请假一周,故博客没有正常更新 RestTemplate介绍: RestTemp ...

  9. Spring Boot使用RestTemplate消费REST服务的几个问题记录

    我们可以通过Spring Boot快速开发REST接口,同时也可能需要在实现接口的过程中,通过Spring Boot调用内外部REST接口完成业务逻辑. 在Spring Boot中,调用REST Ap ...

随机推荐

  1. sql server通过脚本添加链接服务器

    exec sp_addlinkedserver  'ZZSJK','','SQLOLEDB','192.168.10.22'  --链接服务器名称 ‘’ ip地址exec sp_addlinkedsr ...

  2. mongdb aggregate 聚合数据

    最近用到的一些mongodb的数据查询方法 及api用法 Aggregate() 数据聚合处理的方法 可以将聚合的一些方法放在其后面的括号中,也可继续以agg.的样式链式加入 aggregate.al ...

  3. 学习 NLP(一)—— TF-IDF

    TF-IDF(Term Frequency & Inverse Document Frequency),是一种用于信息检索与数据挖掘的常用加权技术.它的主要思想是:如果某个词或短语在一篇文章中 ...

  4. Network Function Virtualization for a Network Device

    An apparatus for performing network function virtualization (NFV), comprising: a memory, a processor ...

  5. C# Winform在win10里弹出无焦点的窗口

    原文:C# Winform在win10里弹出无焦点的窗口 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/wangmy1988/article/det ...

  6. 圆周卷积(circular convolution)

    1. 定义与概念 圆周卷积也叫循环卷积, 2. 实现(matlab) 以圆周的形式卷积两个信号: >> z = ifft(fft(x).*fft(y));

  7. Android Fragment——详细解释

    1.Fragment概述 在一个Activity中. Fragment代表UI的一个部分或者一个行为.一个Activity能够结合多个Fragment对象,也能够在多个activity中使用同样Fra ...

  8. 脚本 启动/停止 jar包服务

    windows (.bat): @set port=8692 @echo %port% for /f "tokens=5" %%i in ('netstat -aon ^| fin ...

  9. 解决Ubuntu14.04在外接显示器不能指定问题的最佳分辨率

    通常这种情况发生.在System Settings -> display 你会发现多出了一个 unknown display. 这往往是因为你使用质量低劣的视频电缆,例如,几美元VGA线. 解决 ...

  10. gnuradio companion 找不到第三方模块gr-osmosdr的问题

    我使用了来自Ettus的gnuradio软件包,之后安装了gr-osmosdr 以在gnuradio中调用RTL电视棒. 但是在gnuradio companion找不到来自rtlsdr-source ...