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

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. DOS批处理

    DOS批处理命令-注释   注释是每个程序中不可或缺的(不是对计算机来说,而是对我们这些程序员阅读代码来说) 语法: ①rem 这是批处理的注释命令,rem后面的内容全部是注释 例:rem 这是一行注 ...

  2. HDU1078 FatMouse and Cheese 【内存搜索】

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  3. QT之圆形头像(使用PNG的Mask达到的效果)

    废话不多说!直接上代码. 我们在很多UI设计应用中,需要用到自定义形状头像,在这里,我对圆形头像的设计做简单的阐述,其它形状头像可参考本文做相应的更改即可.如下图所示为设计的圆形头像: 上代码: Se ...

  4. 数学类网站、代码(Matlab & Python & R)

    0. math & code COME ON CODE ON | A blog about programming and more programming. 1. 中文 统计学Computa ...

  5. 使用nfs映射远程服务器磁盘目录

    参考:http://www.centoscn.com/CentosSecurity/SoftSecurity/2015/0408/5118.html          http://www.cnblo ...

  6. Oracle 如何删除掉一个用户下的所有对象

    create or replace procedure drop_all as cursor cur_obj is select uo.OBJECT_NAME, uo.OBJECT_TYPE from ...

  7. 构建自己的PHP框架(MVC)

    完整项目地址:https://github.com/Evai/Aier 规划文件夹 新建 MFFC/app 文件夹,在 app 中创建 controllers.models.views 三个文件夹,开 ...

  8. maven私服nexus安装

    maven私服nexus安装 1.nexus特性 1.1.nexus私服实际上是一个javaEE的web 系统 1.2.作用:用来管理一个公司所有的jar包,实现项目jar包的版本统一 1.3.jar ...

  9. SwiftCafe 咖啡时光 - 了解 Swift 中的闭包

    闭包(Closure) 是现代开发语言的必备特性,极大的提高了我们的开发效率. 关于闭包,你可以把它理解为一种特殊的变量或对象.简而言之,我们通常的对象,里面存储的是变量或对象的值,而闭包里面存储的是 ...

  10. ASP .NET Controller返回类型

    返回类型 return View(model); 即返回htmlreturn Json("String"); 返回Json格式的数据return File(new byte[] { ...