RestTemplateForBank.java
2.33 KB
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
package com.yohoufo.user.helper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
@Configuration
public class RestTemplateForBank {
private Logger logger = LoggerFactory.getLogger(RestTemplateForBank.class);
/**
* 定义一个500毫秒超时的RestTemplate,调用银联接口用
* @return
*/
@Bean
public RestTemplate authorizeBankRestTemplate(){
/* ResponseErrorHandler responseErrorHandler = new ResponseErrorHandler() {
@Override
public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
int statusCode = getHttpStatusResponseCode(clientHttpResponse);
if(statusCode==200){
return false;
}
return true;
}
@Override
public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
int responseCode = getHttpStatusResponseCode(clientHttpResponse);
logger.error("CustomRestTemplateConfig handleError responseCode {},response {}",responseCode,clientHttpResponse);
}
protected int getHttpStatusResponseCode(ClientHttpResponse response) throws IOException {
try {
return response.getStatusCode().value();
}
catch (IllegalArgumentException ex) {
return 400;
}
}
};*/
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectionRequestTimeout(500);
httpRequestFactory.setConnectTimeout(500);
httpRequestFactory.setReadTimeout(500);
RestTemplate restTemplate= new RestTemplate(httpRequestFactory);
//restTemplate.setErrorHandler(responseErrorHandler);
return restTemplate;
}
}