当前位置: 首页 > 工具软件 > PHP_MVC_REST > 使用案例 >

RestTemplate访问Spring MVC restFul接口

丌官运珧
2023-12-01

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


设置头文件信息,以json格式传送

 类似资料: