在接口請(qǐng)求時(shí),有時(shí)可能因?yàn)榫W(wǎng)絡(luò)或別的問題導(dǎo)致請(qǐng)求不到跨嘉,需要返回個(gè)合適的值繼續(xù)進(jìn)行下面的步驟尊蚁,此時(shí)就需要設(shè)置連接超時(shí)以免報(bào)錯(cuò)亡笑,從而終止。
1.#代碼
private String postForOrderList(OrderListInfoIn orderInfoIn)
{
String elecSaleApiUrl = configBean.getElecSaleApiUrl();
String url = elecSaleApiUrl+"?method=xxx&tid=xxx&access_token="+token+"×tamp="+System.currentTimeMillis()+"&sign=xxxxxxxxxxxxxxxx";
MultiValueMap<String, Object> headers = new LinkedMultiValueMap<String, Object>();
headers.add("Content-Type", "application/json;charset=UTF-8");
HttpEntity request = new HttpEntity(JSON.toJSONString(orderInfoIn),headers);
RestTemplate restTemplate = new RestTemplate();
//設(shè)置超時(shí)
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
clientHttpRequestFactory.setConnectTimeout(8000);
clientHttpRequestFactory.setReadTimeout(8000);
restTemplate.setRequestFactory(clientHttpRequestFactory);
String result = restTemplate.postForObject(url,request,String.class);
return result;
}
2.#此部分拋出異常不處理横朋,在調(diào)用方法時(shí)try...catch仑乌。