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

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. C++重载加号运算符实现两个结构体的相加

    #include<iostream> #include<string> using namespace std; struct S { int a, b; string str ...

  2. Qtcreator中常用快捷键总结(有必要牢记,提高编程效率)

    F1        查看帮助F2        跳转到函数定义(和Ctrl+鼠标左键一样的效果)Shift+F2    声明和定义之间切换F4        头文件和源文件之间切换Ctrl+1     ...

  3. CUDA多个流的使用

    CUDA中使用多个流并行执行数据复制和核函数运算可以进一步提高计算性能.以下程序使用2个流执行运算: #include "cuda_runtime.h" #include < ...

  4. Windows系统的四个重要概念——进程、线程、虚拟内存、内核模式和用户模式

    引言 本来在写一篇Windows内存管理的文章,写着写着就发现好多基础的概念都要先讲.更可怕的是,这些基础的概念我却不能完全讲清楚.只好再把这本<深入解析Windows操作系统>翻到第一章 ...

  5. ios7 左右searchbar在设置cancelButton的title属性

    经 [searchBarsetShowsCancelButton:YES];设置之后默认是"cancel",想改为中文的 在stackoverflow上參考了非常多方法都没用 这里 ...

  6. 微信小程序之商品属性分类

    所提及的购物数量的加减,现在说说商品属性值联动选择. 为了让同学们有个直观的了解,到电商网截了一个图片,就是红圈所示的部分 现在就为大家介绍这个小组件,在小程序中,该如何去写 下图为本项目的图: wx ...

  7. 离散时间信号常见函数的实现(matlab)

    1. 单位样本序列 δ(n−n0)={1,n=n00,n≠n0 function [x, n] = impseq(n0, n1, n2) n = n1:n2; x = [n == n0]; 2. 单位 ...

  8. 怎么快速构建自己的C/C++程序?——有关编译、静态链接和SCons

    怎么快速构建自己的C/C++程序?--有关编译.静态链接和SCons 1. 写在前面 最初写C++是在Visual Studio这个IDE里,那时我并没有makefile的概念,对程序的编译和链接的一 ...

  9. wpf listbox 选中项 上移下移

    原文:wpf listbox 选中项 上移下移 private void MoveUp_Click(object sender, RoutedEventArgs e)         {        ...

  10. springboot 集成oauth2

    未实现.首先实现spring security. 1. 关于oauth2 隐隐觉得集成oauth2,用好它是一个不太简单的事儿,需要对oauth2了解一番. oauth2比较好的参考,都是别人原创文章 ...