Spring Boot(二十):调用REST服务

介绍

pom文件引入:

1
2
3
4
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>

代码实现:

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
@RestController
@RequestMapping(value = "/rest", method = RequestMethod.POST)
public class RestController {

@Autowired
private UserLogCache userLogCache;

@RequestMapping(value = "/update")
public UserLog update(@RequestBody JsonNode jsonNode) {
System.out.println("jsonNode=" + jsonNode);
UserLog bean = userLogCache.selectById(jsonNode.get("id").asInt(1));
if(bean == null){
bean = new UserLog();
}
bean.setUserName("测试");
bean.setCreateTime(new Date());
bean.setUserIp("192.168.1.1");
userLogCache.updateById(bean);
return bean;
}

@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
public UserLog update2(@PathVariable(value = "id") Integer id) {
UserLog bean = userLogCache.selectById(id);
if(bean == null){
bean = new UserLog();
}
bean.setUserName("测试");
bean.setCreateTime(new Date());
bean.setUserIp("192.168.1.1");
userLogCache.updateById(bean);
return bean;
}

}

测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Autowired
//springboot会自动注入RestTemplateBuilder
private RestTemplateBuilder restTemplateBuilder;

/**
* get请求
*/
@Test
public void getForObject() {
UserLog bean = restTemplateBuilder.build().getForObject("http://localhost:8080/rest/update/{id}", UserLog.class, 6);
System.out.println(bean);
Map<String,Object> map = new HashMap<String,Object>();
map.put("id", 7);
bean=restTemplateBuilder.build().postForObject("http://localhost:8080/rest/update", map, UserLog.class);
System.out.println(bean);
}

代理调用

实现类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ProxyCustomizer implements RestTemplateCustomizer {
@Override
public void customize(RestTemplate restTemplate) {
String proxyHost = "119.29.177.120"; //代理ip
int proxyPort = 1080; //代理端口

HttpHost proxy = new HttpHost(proxyHost, proxyPort);
HttpClient httpClient = HttpClientBuilder.create().setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {
@Override
public HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
System.out.println(target.getHostName());//对hostneme做过滤
return super.determineProxy(target, request, context);
}
}).build();
HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
// 连接超时时间
httpComponentsClientHttpRequestFactory.setConnectTimeout(10000);
// 读超时时间
httpComponentsClientHttpRequestFactory.setReadTimeout(60000);
restTemplate.setRequestFactory(httpComponentsClientHttpRequestFactory);
}
}

测试:

1
2
3
4
5
@Test
public void getForObject() {
String result = restTemplateBuilder.additionalCustomizers(new ProxyCustomizer()).build().getForObject("http://www.baidu.com", String.class);
System.out.println(result);
}

在线代理:http://ip.zdaye.com/