spring boot单元测试之RestTemplate(一)
写代码重要,写好的代码准确无误,且符合预期那更是必不可少。
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(一)的更多相关文章
- spring boot单元测试之RestTemplate(二)
上篇博客中,简单介绍了RestTemplate,只是用到了单元测试环节,如果在正式开发环境使用RestTemplate调用远程接口,还有一些配置要做. 一.配置类 由于Spring boot没有对Re ...
- spring boot单元测试之RestTemplate(三)——api详解
本篇内容来自翟永超的<Springcloud微服务实战>,转载请注明. 一.GET请求 在RestTemplate中,对GET请求可以通过如下两个方法进行调用实现. 第一种:getForE ...
- spring boot单元测试之MockMvc
spring单元测试之MockMvc,这个只是模拟,并不是真正的servlet,所以session.servletContext是没法用的. @RunWith(SpringRunner.class) ...
- spring boot项目配置RestTemplate超时时长
配置类: @Configuration public class FeignConfiguration { @Bean(name="remoteRestTemplate") pub ...
- Spring Boot(三):RestTemplate提交表单数据的三种方法
http://blog.csdn.net/yiifaa/article/details/77939282 ********************************************** ...
- Spring Boot 16 条最佳实践
Spring Boot是最流行的用于开发微服务的Java框架.在本文中,我将与你分享自2016年以来我在专业开发中使用Spring Boot所采用的最佳实践.这些内容是基于我的个人经验和一些熟知的Sp ...
- 玩转单元测试之Testing Spring MVC Controllers
玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
spring boot / cloud (八) 使用RestTemplate来构建远程调用服务 前言 上周因家里突发急事,请假一周,故博客没有正常更新 RestTemplate介绍: RestTemp ...
- Spring Boot使用RestTemplate消费REST服务的几个问题记录
我们可以通过Spring Boot快速开发REST接口,同时也可能需要在实现接口的过程中,通过Spring Boot调用内外部REST接口完成业务逻辑. 在Spring Boot中,调用REST Ap ...
随机推荐
- LR杂记 - Linux的系统监控工具vmstat详细说明
一.前言 非常显然从名字中我们就能够知道vmstat是一个查看虚拟内存(Virtual Memory)使用状况的工具,可是如何通过vmstat来发现系统中的瓶颈呢?在回答这个问题前,还是让我们回想一下 ...
- Method for finding shortest path to destination in traffic network using Dijkstra algorithm or Floyd-warshall algorithm
A method is presented for finding a shortest path from a starting place to a destination place in a ...
- Qt 自定义事件(三种方法:继承QEvent,然后Send Post就都可以了,也可以覆盖customEvent函数,也可覆盖event()函数)
Qt 自定义事件很简单,同其它类库的使用很相似,都是要继承一个类进行扩展.在 Qt 中,你需要继承的类是 QEvent. 继承QEvent类,你需要提供一个QEvent::Type类型的参数,作为自定 ...
- Matlab Tricks(十五) —— 圆的正确画法
使用参数方程, phi = 0:0.01:2*pi; x = cos(phi); y = sin(phi); axis equal plot(x, y) 根据参数方程,显然,圆心在 (0, 0),半径 ...
- EMES信息化制造系统的概念
EMES是指,信息化制造系统.它由五个功能子系统构成,分别是:企业信息平台子系统(EIP),物料实时信息系统(MRI).能源实时信息系统(PRI).设备实时信息系统(EMR).质量实时信息系统(QRI ...
- 给 Web 开发人员推荐的通用独立 UI 组件(一)(按钮很不错)
现代 Web 开发在将体验和功能做到极致的同时,对于美观的追求也越来越高.在推荐完图形库之后,再来推荐一些精品的独立 UI 组件.这些组件可组合在一起,形成美观而交互强大的 Web UI . 给 We ...
- 如何控制WAP网站上输入框的默认键盘类型
百度上对这样的资料介绍很多,基本上都和这个页面是一个意思 http://www.w3school.com.cn/html5/att_input_type.asp : 语法 <input type ...
- 重设windows10中的sub linux系统用户密码
原文:重设windows10中的sub linux系统用户密码 版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/haiyoung/article/detai ...
- P和P1指向了O和O1两个变量(对象)的地址, 而不是O和O1的内容(对象的实际地址)——充分证明@是取变量(对象)的地址,而不是变量里面的内容,够清楚!
如图,为什么这样取出来的p,p1的值不一样呢? 165232328群友庾伟洪告诉我: P和P1指向了O和O1两个变量(对象)的地址, 而不是O和O1的内容(对象的实际地址) ,你想P指向真正的对 ...
- 构建自己的PHP框架(路由)
完整项目地址:https://github.com/Evai/Aier 上一篇中我们已经建立了一个空的 Composer 项目,本篇将讲述如何构建路由. 下面我们就开始自己来构建路由,先去 GitHu ...