Spring中使用RestTemplate调用第三方接口API
2017-06-14 17:18
585 查看
外部提供的API接口形式:
调用:http://192.xx.xx.xx:8080/xxxxxx/xxxx?idNo=xxxxx
返回:json字符串
需求:如何在程序中调用外部API接口获取程序所需的对象
解决方法:使用RestTemplate.exchange()方法
什么是RestTemplate?
RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。
调用RestTemplate的默认构造函数,RestTemplate对象在底层通过使用java.net包下的实现创建HTTP 请求,可以通过使用ClientHttpRequestFactory指定不同的HTTP请求方式。
ClientHttpRequestFactory接口主要提供了两种实现方式
一种是SimpleClientHttpRequestFactory,使用J2SE提供的方式(既java.net包提供的方式)创建底层的Http请求连接。
一种方式是使用HttpComponentsClientHttpRequestFactory方式,底层使用HttpClient访问远程的Http服务,使用HttpClient可以配置连接池和证书等信息
使用方法
1. pom.xml添加依赖
2. 使用HttpClient连接池的方式 配置及依赖注入
3. 定义实体类BaseRest,RestResponse
调用序列图
其中核心代码就是restTemplate.exchange()方法
以上,就在spring中使用RestTemplate调用第三方接口的API,将其json内容转换为程序中需要的对象
参考:RestTemplate实践 这篇文章仅仅看懂一点点
调用:http://192.xx.xx.xx:8080/xxxxxx/xxxx?idNo=xxxxx
返回:json字符串
需求:如何在程序中调用外部API接口获取程序所需的对象
解决方法:使用RestTemplate.exchange()方法
什么是RestTemplate?
RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。
调用RestTemplate的默认构造函数,RestTemplate对象在底层通过使用java.net包下的实现创建HTTP 请求,可以通过使用ClientHttpRequestFactory指定不同的HTTP请求方式。
ClientHttpRequestFactory接口主要提供了两种实现方式
一种是SimpleClientHttpRequestFactory,使用J2SE提供的方式(既java.net包提供的方式)创建底层的Http请求连接。
一种方式是使用HttpComponentsClientHttpRequestFactory方式,底层使用HttpClient访问远程的Http服务,使用HttpClient可以配置连接池和证书等信息
使用方法
1. pom.xml添加依赖
<!--调用外部网络API使用 start--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5</version> </dependency> <!--调用外部网络API使用 end--> <!--Rest请求start--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.8.8</version> </dependency> <!--rest请求end-->
2. 使用HttpClient连接池的方式 配置及依赖注入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName" default-lazy-init="true"> <!--方式二、使用httpclient的实现,带连接池--> <bean id="ky.pollingConnectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager"> <!--整个连接池的并发--> <property name="maxTotal" value="1000"/> <!--每个主机的并发--> <property name="defaultMaxPerRoute" value="1000"/> </bean> <bean id="ky.httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder" factory-method="create"> <property name="connectionManager" ref="ky.pollingConnectionManager"/> <!--开启重试--> <property name="retryHandler"> <bean class="org.apache.http.impl.client.DefaultHttpRequestRetryHandler"> <constructor-arg value="2"/> <constructor-arg value="true"/> </bean> </property> <property name="defaultHeaders"> <list> <bean class="org.apache.http.message.BasicHeader"> <constructor-arg value="User-Agent"/> <constructor-arg value="Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36"/> </bean> <bean class="org.apache.http.message.BasicHeader"> <constructor-arg value="Accept-Encoding"/> <constructor-arg value="gzip,deflate"/> </bean> <bean class="org.apache.http.message.BasicHeader"> <constructor-arg value="Accept-Language"/> <constructor-arg value="zh-CN"/> </bean> </list> </property> </bean> <bean id="ky.httpClient" factory-bean="ky.httpClientBuilder" factory-method="build"/> <bean id="ky.clientHttpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"> <constructor-arg ref="ky.httpClient"/> <!--连接超时时间,毫秒--> <property name="connectTimeout" value="5000"/> <!--读写超时时间,毫秒--> <property name="readTimeout" value="10000"/> </bean> <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> <constructor-arg ref="ky.clientHttpRequestFactory"/> <property name="errorHandler"> <bean class="org.springframework.web.client.DefaultResponseErrorHandler"/> </property> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.FormHttpMessageConverter"/> <bean class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> <bean id="baseRest" class="com.hfi.common.dao.BaseRest"> <property name="restTemplate" ref="restTemplate"/> <property name="restServer" value="{'xxxx':['http://192.xx.xx.xx:8080/xxxx']}"/> </bean> </beans>
3. 定义实体类BaseRest,RestResponse
调用序列图
其中核心代码就是restTemplate.exchange()方法
以上,就在spring中使用RestTemplate调用第三方接口的API,将其json内容转换为程序中需要的对象
参考:RestTemplate实践 这篇文章仅仅看懂一点点
相关文章推荐
- SpringBoot系列十一:SpringBoot整合Restful架构(使用 RestTemplate 模版实现 Rest 服务调用、Swagger 集成、动态修改日志级别)
- SpringCloud 查找调用REST服务使用RestTemplate(ribbon负载)或feign模式 教程源码 火推
- spring rest mvc使用RestTemplate调用
- APP server 使用httpclient调用第三方环信的server rest接口获取token
- RestTemplate使用gzip压缩调用接口
- 如何使用python3调用openstack keystone identity REST api接口获取X-AUTH-TOKEN
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- ribbon通过RestTemplate调用接口获取List<JavaBean>时,应使用数组而不能直接使用List
- 使用restTemplate调用外部接口
- spring rest mvc使用RestTemplate调用
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- 使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
- 使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
- 使用RestTemplate实现rest服务的调用
- DBUnit单元测试用restTemplate调用接口出现unrecognized token verifyFail:was expection('true','false' or 'null')问题
- Spring RestTemplate使用ByteArrayResource上传方式
- 使用RestTemplate Spring安全认证
- 使用Spring Boot和Kotlin创建RESTfull API
- 第四章 Spring CLoud 使用RestTemplate+Eureka调用用户微服务
- Atitit 图像处理之编程之类库调用的接口api cli gui ws rest attilax大总结.docx