在我的Sping Boot 微服务中,我使用OAuth2 RestTemplate进行rest API调用。现在,我们必须在rest API调用中添加请求超时,为此,我在www.example.com文件中尝试spring.mvc.mvc.request-timeout=20000application.properties,但没有工作。
@Configuration
public class ResourceConfig {
@Bean
@LoadBalanced
@Qualifier("oauth2BalancedRestTemplate")
public OAuth2RestTemplate oauth2BalancedRestTemplate(@Qualifier("oauth2RestTemplate") OAuth2RestTemplate bean) {
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(bean.getResource());
restTemplate.setRequestFactory(bean.getRequestFactory());
return restTemplate;
}
@Bean
@LoadBalanced
@Qualifier("oauth2JWTBalancedRestTemplate")
public OAuth2RestTemplate oauth2JWTBalancedRestTemplate(@Qualifier("oauth2JWTRestTemplate") OAuth2RestTemplate bean) {
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(bean.getResource());
restTemplate.setRequestFactory(bean.getRequestFactory());
return restTemplate;
}
}
@Service
public class ApiCall {
@Value("${url}")
private String uri;
@Value("${endpoint}")
private String endpoint;
@Autowired
@Qualifier("oauth2JWTBalancedRestTemplate")
private OAuth2RestTemplate restTemplate;
public Response getTrdDetail(Request req) {
HttpHeaders headers = new HttpHeaders();
HttpEntity<Request> httpEntity = new HttpEntity<Request> (req, headers);
return restTemplate.exchange(uri + endpoint, HttpMethod.POST, httpEntity, Response.class);
}
}
1条答案
按热度按时间7qhs6swi1#
确保这些依赖关系
配置HttpRequestFactory bean,然后将其注入到OAuth2RestTemplate bean中: