RestTemplateConfig

RestTemplateConfig 获取第三方接口数据

1
2
3
4
5
6
7
8
9
10
11
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

1.Maven坐标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>

<dependencies>
<!--CloseableHttpClient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>

<!--spring restTemplate-->
<!-- @ConfigurationProperties annotation processing (metadata for IDEs)
生成spring-configuration-metadata.json类,需要引入此类-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

2.在启动类同包下创建RestTemplateConfig.java类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.bohu;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
* @ClassName RestTemplateConfig
* @Author shenbohu
* @Date 2021/12/2310:18 上午
* @Version 1.0
**/
@Configuration
public class RestTemplateConfig {


@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}

@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(15000);
factory.setReadTimeout(5000);
return factory;
}
}

3.在Service类中注入使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.bohu.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bohu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import com.bohu.entity.PageResult;
import com.bohu.entity.YmlConfig;

import com.bohu.service.HJ212Service;
import lombok.Data;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;


/**
* @ClassName UserServiceImpl
* @Author shenbohu
* @Date 2021/5/1910:20 上午
* @Version 1.0
**/
@Service
@Transactional
@Configuration
@Data
public class HJ212ServiceImpl implements HJ212Service {


@Autowired
private RestTemplate restTemplate;

@Autowired
private YmlConfig ymlConfig;

@Override
public PageResult gethj212() {
System.out.println("ccccccccc");
String json = doGetWith1(String.valueOf(11));
System.out.println(json);
return null;
}

/**
* 以get方式请求第三方http接口 getForEntity
* @param url
* @return
*/
public String doGetWith1(String url){
url = "https://cover.yixinda.com/cover-device?companyId=111";
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
String body = responseEntity.getBody();
return body;
}

/**
* 以get方式请求第三方http接口 getForObject
* 返回值返回的是响应体,省去了我们再去getBody()
* @param url
* @return
*/
public User doGetWith2(String url){
User user = restTemplate.getForObject(url, User.class);
return user;
}

/**
* 以post方式请求第三方http接口 postForEntity
* @param url
* @return
*/
public String doPostWith1(String url){
User user = new User();
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, user, String.class);
String body = responseEntity.getBody();
return body;
}

/**
* 以post方式请求第三方http接口 postForEntity
* @param url
* @return
*/
public String doPostWith2(String url){
User user = new User();
String body = restTemplate.postForObject(url, user, String.class);
return body;
}

/**
* exchange
* @return
*/
public String doExchange(String url, Integer age, String name){
//header参数
HttpHeaders headers = new HttpHeaders();
String token = "asdfaf2322";
headers.add("authorization", token);
headers.setContentType(MediaType.APPLICATION_JSON);

//放入body中的json参数
JSONObject obj = new JSONObject();
obj.put("age", age);
obj.put("name", name);

//组装
HttpEntity<JSONObject> request = new HttpEntity<>(obj, headers);
String s = request.toString();
return s;
}


}

上次更新 2021-12-26