使用 RestTemplate 调用 restful 服务
什么是RestTemplate?
RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。
调用RestTemplate的默认构造函数,RestTemplate对象在底层通过使用java.net包下的实现创建HTTP 请求,可以通过使用ClientHttpRequestFactory指定不同的HTTP请求方式。
ClientHttpRequestFactory接口主要提供了两种实现方式
- 一种是SimpleClientHttpRequestFactory,使用J2SE提供的方式(既java.net包提供的方式)创建底层的Http请求连接。
- 一种方式是使用HttpComponentsClientHttpRequestFactory方式,底层使用HttpClient访问远程的Http服务,使用HttpClient可以配置连接池和证书等信息。
RestTemplate默认是使用SimpleClientHttpRequestFactory,内部是调用jdk的HttpConnection,默认超时为-1
使用示例:
- 启动程序
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
public class SpringbootRestTemplateApplication { // 启动的时候要注意,由于我们在controller中注入了RestTemplate,所以启动的时候需要实例化该类的一个实例
@Autowired
private RestTemplateBuilder builder; // 使用RestTemplateBuilder来实例化RestTemplate对象,spring默认已经注入了RestTemplateBuilder实例
@Bean
public RestTemplate restTemplate() {
return builder.build();
} public static void main(String[] args) {
SpringApplication.run(SpringbootRestTemplateApplication.class, args);
} } - 编写controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import com.chhliu.springboot.restful.vo.User; @RestController
public class RestTemplateController { @Autowired
private RestTemplate restTemplate; @GetMapping("/template/{id}")
public User findById(@PathVariable Long id) {
//http://localhost:8080/user/ 是服务的对应的url
User u = this.restTemplate.getForObject("http://localhost:8080/user/" + id,User.class);
System.out.println(u);
return u;
}
}
更多使用语法请查看API文档
使用 RestTemplate 调用 restful 服务的更多相关文章
- chrome插件 postman 可以调用restful服务
chrome插件 postman 可以调用restful服务
- RestTemplate 调用本地服务 connection refused
当需要使用服务间的互相调用的时候,通常来说最优雅的方式莫过于Feign调用了.但是有时候特殊原因还是需要使用httpClient之类的工具. 本次我在使用RestTemplate调用本地服务的时候,会 ...
- 如何使用RestTemplate访问restful服务
一. 什么是RestTemplate 传统情况下在java代码里访问restful服务,一般使用Apache的HttpClient.不过此种方法使用起来太过繁琐.spring提供了一种简单便捷的模板类 ...
- Java方法通过RestTemplate调用restful接口
背景:项目A需要在代码内部调用项目B的一个restful接口,该接口是POST方式,header中 Authorization为自定义内容,主要传输的内容封装在body中,所以使用到了RestTemp ...
- C# 调用restful服务开源库
.NET环境下我们想调用其它开放平台的服务接口,不需要自己去实现底层,开源的库用起来会很方便 hammock http://www.cnblogs.com/shanyou/archive/2012/0 ...
- 如何在Java客户端调用RESTful服务
在这个例子中,我们将看到如何使用java.net包实用工具,创建一个访问REST服务RESTful的客户端.当然这不是创建一个RESTful客户端最简单的方法,因为你必须自己读取服务器端的响应,以及J ...
- 使用RestTemplate访问restful服务时遇到的问题
可以通过通过wireshark抓包,使用Postman发送请求 wireshark是非常流行的网络封包分析软件,功能十分强大.可以截取各种网络封包,显示网络封包的详细信息.使用wireshark的人必 ...
- SpringBoot:使用feign调用restful服务时地址栏传参
1.服务提供者(controller层) @GetMapping("/user/{id}") public ApiResult getById(@PathVariable(&quo ...
- 使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
在 Spring 3.0 中可以通过 HttpEntity 对象自定义请求头信息,如: private static final String APPLICATION_PDF = "app ...
随机推荐
- topshelf 开发windows 服务资料
官方配置 http://docs.topshelf-project.com/en/latest/configuration/config_api.html#service-start-modes to ...
- 通过socks tunnel设置http代理
1. 在服务器上使用ssh创建tunnel -f -C -q -N milton@112.34.113.35 # -D: 让ssh在指定的端口(-)上建立一个 SOCKS tunnel # -f: 让 ...
- 【highstock】按时间(zoom)让它去访问服务器呢?
$(function () { /** * Load new data depending on the selected min and max */ function afterSetExtrem ...
- 共享内存:mmap函数实现
内存映射的应用: 以页面为单位,将一个普通文件映射到内存中,通常在须要对文件进行频繁读写时使用,这样用内存读写代替I/O读写,以获得较高的性能; 将特殊文件进行匿名内存映射,能够为关联进程提供共享内存 ...
- How to Land your Dream Job
今天在code school上面看到一个外国人写的文章,写的很棒,瞧一下外国人的思维和我们有什么不同?分享给所有的朋友 You know how some things ar ...
- Java概述与开发环境
1.Java语言既是编译又是解释型的特点 Java没有设计成编译型语言,因为编译型语言效率高, 但可移植性差. Java也没有设计成纯解释型的语言, 因为解释型效率太低. 编译成class文件, 解释 ...
- sid-msg.map文件概述
我这边编写了magic对应的指定文件规则,但是运行的时候发现储存的文件中包含我未指定的数据文件: 在rules下边看的时候,发现有sid-msg.map文件,上网了解下这个文件是干啥的.. 下边文章来 ...
- iCheck的全选和获取value
一.全选 在使用jQuery iCheck 插件的时候遇到了一个问题,就是当我们使用普通的js全选功能无效了. $("#checkall").click( function(){ ...
- IDEA使用笔记(三)——小齿轮的显示和隐藏(Autoscroll from Source)
在玩快捷键的时候,不清楚自己操作了什么,突然间发现——能直接定位到当前可编辑文件的哪个小齿轮,不见了,找了一会也没弄出来,从网上搜索吧!也没看到对应的方法,后来自己耐下心来复盘自己的操作,终于发现了, ...
- linux手工释放内存
先使用sync命令以确保文件系统的完整性,sync 命令运行 sync 子例程,将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node.已延迟的块 I/O 和读写映射文件. 再执行下面任意一条命 ...