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

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. Ibatis之RowHandler

    如果一个场景:账户表中有1千万账户,现在,我们需要这个1千万账户利息结算业务.需求是基于Ibatis框架来实现这个功能. 如果按照一般的编程模式,我们将编写一个sql,然后调用QueryForList ...

  2. zabbix 设备(自己的实践)

    1. 下载源代码包 wget http://sourceforge.net/projects/zabbix/files/ 2.  解压 tar -zxvf zabbix-2.2.3.tar.gz 3. ...

  3. ASP.NET Core Identity 框架 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core Identity 框架 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Identity 框架 前面我们使用了 N 多个章节, ...

  4. 构建自己的PHP框架(Redis)

    完整项目地址:https://github.com/Evai/Aier Redis 简介 'Redis' 是一个高性能的 'key-value' 数据库,其 'value' 支持 'String'.' ...

  5. ShopNC本地生活o2o网站的源代码,没有域名限制

    较前某VIP源代码论坛分享了套ShopNC本地生活o2o站点系统.下载过来却发现根本不能用,所以一直没分享出来.今天咱们这边分享的这套ShopNC本地生活o2o站点源代码,无不论什么的限制,直接ins ...

  6. EJB什么,它真的有这么神奇??

    1. 我们不禁要问,什么是"服务集群"?什么是"企业发展"? 现在说EJB 至"服务集群"和"企业发展",然后,说什么是 ...

  7. 一款天气app的温度曲线图的实现

    原文:一款天气app的温度曲线图的实现 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/tyhzsd/article/details/50544639 ...

  8. WPF里的一些Effect特效

    原文:WPF里的一些Effect特效 Blend的特效都在Microsoft.Expression.Media.Effects里,用之前添加一下引用. 可以在前台选中对象后直接点击Effect新建一种 ...

  9. WebAPI Delete方法报错405 Method Not Allowed

    .net framework 在Web.config文件中添加如下配置: <system.webServer> <modules runAllManagedModulesForAll ...

  10. linux C 内存管理方式之半动态

    看到半动态申请内存,第一反应这是什么鬼? 实际上半动态内存申请很容易理解,在GNU C中使用alloca函数来实现 #include <stdlib.h> void *alloca (si ...