在Spring MVC中,访问restful接口:
RestTemplate restTemplate=new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
//设置连接超时时间
requestFactory.setConnectTimeout(1000);
//设置读取超时时间
requestFactory.setReadTimeout(1000);
restTemplate.setRequestFactory(requestFactory);
JSONObject json=new JSONObject(param);
HttpHeaders httphead=new HttpHeaders();
httphead.setContentType(MediaType.parseMediaType("application/json; charset=UTF-8"));
httphead.add(HttpHeaders.ACCEPT,MediaType.APPLICATION_JSON.toString()); ;
HttpEntity<String> entity=new HttpEntity<String>(json.toString(),httphead);
String types= restTemplate.postForObject(url, entity,String.class);