1. 程式人生 > >I need you back to stay

I need you back to stay

RestTemplate它簡化了與HTTP伺服器通訊,執行基於rest的原則。它處理HTTP連線,使應用程式程式碼提供的url(可能的模板變數)和提取結果。
RestTemplate提供了一系列呼叫spring mvc rest(或者說 spring rest webservice)介面 可以呼叫http請求的WebService,並將結果轉換成相應的物件型別。


基本方法 常用方法 get
url 請求地址 responseType 返回值型別
getForEntity(url, responseType);

引數地址 url, 返回值型別 Class<T> responseType, 引數可變引數  可以是map 、Object... urlVariables
rest.getForEntity(url, responseType, urlVariables)

引數url 返回值型別 引數型別為map
getForEntity(url, responseType,  Map<K, V)

引數地址 url, 返回值型別 Class<T> responseType, 引數可變引數  可以是map 、Object... urlVariables
getForObject(url, responseType,  Map<K, V) 類似方法引數與getForEntity相同  只是返回值不同 看下例子

Controller 類容方法如下:

<span style="font-size:14px;">@Controller
public class TestRestTemplate {

	/**
	 * get請求
	 * 返回引數自定義 這裡返回什麼引數客戶端接收什麼引數
	 * @return
	 */
	@RequestMapping(value="/restget/{param}/get/{red}",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> restGetRequest(@PathVariable String param,
			@PathVariable Integer red){
		Map<String, Object> map =new HashMap<String, Object>();
		map.put("param", param);
		map.put("red", red);
		System.out.println(param);
		return map;
	}
	
	/**
	 * 返回字串
	 * @param param
	 * @param red
	 * @return
	 */
	@RequestMapping(value="/restpath/{param}/get/{red}",method=RequestMethod.GET)
	@ResponseBody
	public String rest(@PathVariable String param,
			@PathVariable Integer red){
		System.out.println(red);
		return param+":"+red;
	}
}</span>
看看客戶端呼叫:
<span style="font-size:14px;">public class WebRestTemplate {
	
	 RestTemplate  rest=new RestTemplate();

	/**
	 * 呼叫getForEntity 實現get請求 看引數設定 
	 * @return
	 */
	 public Map<String, Object> getForEntity1(){
		String url="http://localhost:8080/share-web/restget/www/get/2"; 
		ResponseEntity<Map>  map=rest.getForEntity(url, Map.class);
		Map<String, Object> maps=map.getBody();
		System.out.println(maps);
		return maps;
	 }
	 
	 /**
	  * rest.getForEntity方法 看引數設定 可變引數
	  * @return
	  */
	 public Map<String, Object> getForEntity2(){
			 String url="http://localhost:8080/share-web/restget/{param}/get/{red}"; 
			 ResponseEntity<Map>  map=rest.getForEntity(url, Map.class, "www",1);
			Map<String, Object> maps=map.getBody();
			return maps;
	 }
	 
	 /**
	  * test 引數返回字串
	  * map為引數
	  * @return
	  */
	 public String  getForEntity3(){
		 String url="http://localhost:8080/share-web/restpath/{param}/get/{red}";
		 Map<String, Object> m=new HashMap<String, Object>();
		 m.put("param", "test");
		 m.put("red", 3);
		ResponseEntity<String>  map=rest.getForEntity(url, String.class,m);
		String str=map.getBody();
		return str;
	 }

	 /**
	  * 有時候後我們需要設定頭部資訊 本人這次就上當了搞了好的半天
	  *	能用於基於HttpEntity 設定頭
	  * exchange()方法可以用於新增請求和響應頭
	  * @param url
	  * @param param
	  * @return
	  */
	 public String getRequest(){
		 String url="http://localhost:8080/share-web/restpath/{param}/get/{red}";
		 HttpHeaders requestHeaders = new HttpHeaders(); 
		 requestHeaders.set("head", "xxx");
		 Map<String, Object> m=new HashMap<String, Object>();
		 m.put("param", "test");
		 m.put("red", 3);
//		 requestHeaders.setAccept(acceptableMediaTypes);
//		 requestHeaders.setContentType(MediaType.APPLICATION_JSON);
		 HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);  
		 HttpEntity<String> response = rest.exchange(  
			        url,  
			        HttpMethod.GET, requestEntity, String.class, m);  
			String str=response.getBody();
			return str;
	 }
	 
	 public static void main(String[] args) {
		 WebRestTemplate web=new WebRestTemplate();
		
		 Map<String, Object> map=web.getForEntity1();
		 System.out.println(map);
		
		 Map<String, Object> maps=web.getForEntity2();
		 System.out.println(maps);
		 
		 String str=web.getForEntity3();
		 System.out.println(str);
		 
		 
		 String str2=web.getRequest();
		 System.out.println(str2);
	}
}</span>

接下來看看 post中的那些方法 常用的就這兩種 其他的都一樣
url地址 request請求引數【比如 這引數設定後面的引數 可以不用引數】 responsType 返回值型別 請求引數  


postForObject(url, request, responseType, uriVariables);


postForEntity(url, request, responseType, uriVariables);

Controller 類容方法如下:

<span style="font-size:14px;">@Controller
public class TestRestTemplate {

	@RequestMapping(value="/restpost",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> restPost(@RequestBody Map<String,Object> map){
		
		System.out.println(map);
		map.put("aaa", "ffffff");
		return map;
	}
}</span>

看看客戶端呼叫:
<span style="font-size:14px;">public class WebRestTemplate {
	
	 RestTemplate  rest=new RestTemplate();

	 public Map<String, Object> postForEntity(){
		 
		 String url="http://localhost:8080/share-web/restpost";
		 //設定head
		 HttpHeaders requestHeaders = new HttpHeaders(); 
		 requestHeaders.set("head", "password"); 
		 requestHeaders.setContentType(MediaType.APPLICATION_JSON);
		 requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
		 Map<String, Object> m=new HashMap<String, Object>();
		 m.put("param", "test");
		 m.put("red", 3);
		 //通過HttpEntity 設定引數 head
		 HttpEntity<?> request=new HttpEntity(m, requestHeaders);
		 //url地址 request請求引數 responsType 返回值型別 請求引數  
		 ResponseEntity<Map> entitymap= rest.postForEntity(url, request, Map.class);
		 Map<String, Object> map=entitymap.getBody();
		 return map;
	 }
	 
	 public Map<String, Object> postObject(){
		 
		 String url="http://localhost:8080/share-web/restpost";
		 //設定head
		 HttpHeaders requestHeaders = new HttpHeaders(); 
		 requestHeaders.set("head", "password"); 
		 requestHeaders.setContentType(MediaType.APPLICATION_JSON);
		 requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
		 Map<String, Object> m=new HashMap<String, Object>();
		 m.put("param", "test");
		 m.put("red", 3);
		 //通過HttpEntity 設定引數 head
		 HttpEntity<?> request=new HttpEntity(m, requestHeaders);
		 //url地址 request請求引數 responsType 返回值型別 請求引數  
		 Map<String, Object> mapobj= rest.postForObject(url, request, Map.class);
		 return mapobj;
	 }
	 
	 public static void main(String[] args) {
		 WebRestTemplate web=new WebRestTemplate();
		
		 Map<String, Object> map=web.postObject();
		 System.out.println(map);
		
		 Map<String, Object> maps=web.postForEntity();
		 System.out.println(maps);
	}
}</span>

搞了很久 終於搞定了  哈哈哈哈哈哈