2017-04-26 58 views
1
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; 
    } 

而不是httpclient,我必須使用rest模板。 http客戶端運行正常。想要使用服務使用休息模板的post方法

我已經嘗試了其他模板的方法如下:

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(); 
} 

想休息模板如何才能實現配置超時。

+1

格式代碼正確 – pvpkiran

回答

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); 
    } 

和自動裝配RestTemplate,而不是使用新創建..

上述所有超時是在milleseconds。

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; 
    } 
} 
+0

的基於XML的配置是這樣的: <構造精氨酸>

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; 

    } 
相關問題