public class CommonMethods {
final Logger logger = (Logger) LoggerFactory.getLogger(CommonMethods.class);
@Autowired
HttpClientFactory httpClientFactory;
enter code here
@Autowired
JSONCreation jsonCreation;
String responseAsStringEmployment;
public String getEmploymentDetails(EmpProfile empProfile) throws IOException, JSONException {
HttpClient clientEmployment = httpClientFactory.getHttpClient();
String urlEmployment = Config.getDefaultCommonServiceURL();
HttpPost requestEmployment = new HttpPost(urlEmployment + "getEmploymentDetails");
requestEmployment.addHeader("content-type", "application/json");
StringEntity params;
String a = jsonCreation.createJsonforNode(empProfile);
params = new StringEntity(a);
requestEmployment.setEntity(params);
logger.info("Request Employment :" + requestEmployment);
logger.info("clientEmployment :" + clientEmployment);
HttpResponse responseEmployment = clientEmployment.execute(requestEmployment);
logger.info("responseEmployment :" + responseEmployment);
responseAsStringEmployment = EntityUtils.toString(responseEmployment.getEntity());
logger.info("Response Employment : " + responseAsStringEmployment);
logger.info("responseAsStringEmployment " + responseAsStringEmployment);
return responseAsStringEmployment;
}
我必须使用rest模板而不是httpclient。 http客户端正常运行。
我为休息模板尝试过的方法如下:
public String getEmployment() {
String url = "https://empprofile-blue.bdt.com/getEmploymentDetails";
EmpProfile empProfile = new EmpProfile();
RestTemplate restTemplate = new RestTemplate();
EmpProfile result = restTemplate.postForObject(url, empProfile, EmpProfile.class);
System.out.println(result);
logger.info(empProfile.toString());
return result.toString();
}
想要在休息模板中配置超时如何实现。
答案 0 :(得分:1)
@Autowired
RestTemplate restTemplate;
@Autowired
HttpClientFactory httpClientFactory;
public String getPassportDetails(EmpProfile empProfile) throws JSONException {
String url = Config.getDefaultCommonServiceURL();
String a = jsonCreation.createJsonforNode(empProfile);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(a, headers);
((HttpComponentsClientHttpRequestFactory)this.restTemplate.getRequestFactory()).setReadTimeout(1 * 5000);//timeout
String result = restTemplate.postForObject(url, entity, String.class);
logger.info(result);
logger.info(empProfile.toString());
return result;
}
答案 1 :(得分:0)
添加像这样的bean
@Bean
public RestTemplate restTemplateForEmployment(){
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
CloseableHttpClient httpClient = httpClientBuilder.build();
HttpComponentsClientHttpRequestFactory rf = new HttpComponentsClientHttpRequestFactory(httpClient);
rf.setConnectTimeout(1000);
rf.setConnectionRequestTimeout(1000);
rf.setReadTimeout(10000);
return new RestTemplate(rf);
}
和Autowire RestTemplate,而不是使用new ..
创建所有上述超时都是以毫秒为单位。
答案 2 :(得分:0)
@Configuration
public class MyAppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(clientHttpRequestFactory());
}
private ClientHttpRequestFactory clientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(2000);
factory.setConnectTimeout(2000);
return factory;
}
}