通过SpringBoot-RestTemplate
delete() 在特定的URL上对资源执行HTTP DELETE操作
exchange() 在URL上执行特定的HTTP方法,返回包含对象的ResponseEntity,这个对象是从响应体中映射得到的
execute() 在URL上执行特定的HTTP方法,返回一个从响应体映射得到的对象
getForEntity() 发送一个HTTP GET请求,返回的ResponseEntity包含了响应体所映射成的对象
getForObject() 发送一个HTTP GET请求,返回的请求体将映射为一个对象
postForEntity() POST 数据到一个URL,返回包含一个对象的ResponseEntity,这个对象是从响应体中映射得到的
postForObject() POST 数据到一个URL,返回根据响应体匹配形成的对象
headForHeaders() 发送HTTP HEAD请求,返回包含特定资源URL的HTTP头
optionsForAllow() 发送HTTP OPTIONS请求,返回对特定URL的Allow头信息
postForLocation() POST 数据到一个URL,返回新创建资源的URL
put() PUT 资源到特定的URL
导入springboot的web包
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
org.apache.httpcomponents
httpclient
4.5.2
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.boot
spring-boot-starter-aop
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.springframework.boot
spring-boot-starter-jetty
org.springframework.boot
spring-boot-starter-test
test
在启动类同包下创建RestTemplateConfig.java类
importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.http.client.ClientHttpRequestFactory;importorg.springframework.http.client.SimpleClientHttpRequestFactory;importorg.springframework.web.client.RestTemplate;
@Configurationpublic classRestTemplateConfig {
@BeanpublicRestTemplate restTemplate(ClientHttpRequestFactory factory){return newRestTemplate(factory);
}
@BeanpublicClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory= newSimpleClientHttpRequestFactory();
factory.setConnectTimeout(15000);
factory.setReadTimeout(5000);returnfactory;
}
}
然后在Service类(RestTemplateToInterface )中注入使用
importcom.alibaba.fastjson.JSONObject;importcom.swordfall.model.User;importorg.springframework.beans.factory.annotation.Autowired;import org.springframework.http.*;importorg.springframework.stereotype.Service;importorg.springframework.web.client.RestTemplate;
@Servicepublic classRestTemplateToInterface {
@AutowiredprivateRestTemplate restTemplate;/*** 以get方式请求第三方http接口 getForEntity
*@paramurl
*@return
*/
publicUser doGetWith1(String url){
ResponseEntity responseEntity = restTemplate.getForEntity(url, User.class);
User user=responseEntity.getBody();returnuser;
}/*** 以get方式请求第三方http接口 getForObject
* 返回值返回的是响应体,省去了我们再去getBody()
*@paramurl
*@return
*/
publicUser doGetWith2(String url){
User user= restTemplate.getForObject(url, User.class);returnuser;
}/*** 以post方式请求第三方http接口 postForEntity
*@paramurl
*@return
*/
publicString doPostWith1(String url){
User user= new User("小白", 20);
ResponseEntity responseEntity = restTemplate.postForEntity(url, user, String.class);
String body=responseEntity.getBody();returnbody;
}/*** 以post方式请求第三方http接口 postForEntity
*@paramurl
*@return
*/
publicString doPostWith2(String url){
User user= new User("小白", 20);
String body= restTemplate.postForObject(url, user, String.class);returnbody;
}/*** exchange
*@return
*/
publicString doExchange(String url, Integer age, String name){//header参数
HttpHeaders headers = newHttpHeaders();
String token= "asdfaf2322";
headers.add("authorization", token);
headers.setContentType(MediaType.APPLICATION_JSON);//放入body中的json参数
JSONObject obj = newJSONObject();
obj.put("age", age);
obj.put("name", name);//组装
HttpEntity request = new HttpEntity<>(obj, headers);
ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
String body=responseEntity.getBody();returnbody;
}
}