restTemplate
是spring
提供的可以提供訪問rest服務的客戶端工具類,提供多種快捷的訪問遠程的方法,大大提高了客戶端的編程效率。解放了原先HttpClient
的複雜提交。
藉助 RestTemplate
,Spring
應用能夠方便地使用REST資源,Spring
的 RestTemplate
訪問使用了模版方法的設計模式。
模版方法將過程中與特定實現相關的部分委託給接口,而這個接口的不同實現定義了接口的不同行爲。
RestTemplate定義了36個與REST資源交互的方法,其中的大多數都對應於HTTP的方法。
其實,這裏面只有11個獨立的方法,其中有十個有三種重載形式,而第十一個則重載了六次,這樣一共形成了36個方法。
delete() 在特定的URL上對資源執行HTTP DELETE操作exchange()
在URL上執行特定的HTTP方法,返回包含對象的ResponseEntity,這個對象是從響應體中
映射得到的execute() 在URL上執行特定的HTTP方法,返回一個從響應體映射得到的對象
getForEntity() 發送一個HTTP GET請求,返回的ResponseEntity包含了響應體所映射成的對象
getForObject() 發送一個HTTP GET請求,返回的請求體將映射爲一個對象
postForEntity()
POST 數據到一個URL,返回包含一個對象的ResponseEntity,這個對象是從響應體中映射得
到的postForObject() POST 數據到一個URL,返回根據響應體匹配形成的對象
headForHeaders() 發送HTTP HEAD請求,返回包含特定資源URL的HTTP頭
optionsForAllow() 發送HTTP OPTIONS請求,返回對特定URL的Allow頭信息
postForLocation() POST 數據到一個URL,返回新創建資源的URL
put() PUT 資源到特定的URL
也就是兩種請求,get
請求和post
請求。
一,get
請求
1,getForObject
用法
消費者發起請求
/**
* @title
* @description get請求 getForObject用法
* @author FanJiangFeng
* @updateTime 2019/12/9 15:32
* @throws
*/
@RequestMapping("/go")
@ResponseBody
public String studyHttp(){
String url="http://127.0.0.1:8082/demoTest?name={name}&email={email}";
Map<String,String> map=new HashMap<String, String>();
map.put("name","樊江鋒");
map.put("email","[email protected]");
RestTemplate restTemplate=new RestTemplate();
String response = restTemplate.getForObject(url, String.class, map);
log.info("response:"+response);
//[nio-8088-exec-1] com.example.controller.HttpController : response:success
return response;
}
提供者接收並響應請求
/**
* @title
* @description get請求 getForObject用法
* @author FanJiangFeng
* @updateTime 2019/12/9 15:32
* @throws
*/
@RequestMapping(value = "demoTest", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String demoTest(@RequestParam String name,@RequestParam String email){
System.out.println(name);
System.out.println(email);
return "success";
}
2,getForEntity
用法
消費者發起請求
/**
* @title
* @description get請求 getForEntity用法
* @author FanJiangFeng
* @updateTime 2019/12/11 16:19
* @throws
*/
@RequestMapping("/getTest")
@ResponseBody
public String getTest(){
String url="http://127.0.0.1:8082/demoTest1?name={name}&email={email}";
// HttpHeaders headers=new HttpHeaders();
// headers.set("phone","123456");
Map<String,Object> map=new HashMap<String, Object>();
map.put("name","樊江鋒");
map.put("email","[email protected]");
RestTemplate restTemplate = new RestTemplate();
// HttpEntity httpEntity = new HttpEntity(headers);
ResponseEntity<String> response = restTemplate.getForEntity(url,String.class,map);
log.info("response:"+response.getBody());
String body = response.getBody();
return body;
}
提供者接收並響應請求
/**
* @title
* @description get請求 getForEntity用法
* @author FanJiangFeng
* @updateTime 2019/12/11 16:19
* @throws
*/
@GetMapping(value = "demoTest1", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String demoTest1(@RequestParam String name, @RequestParam String email){
System.out.println(name);
System.out.println(email);
return "success";
}
二,post
請求
當所有傳遞的數據類型爲複雜數據類型headers.setContentType(MediaType.MULTIPART_FORM_DATA
並且需要接受的參數不再實體類中進行映射。MultiValueMap
來進行傳遞。
消費者發起請求
/**
* @title
* @description post請求
* @author FanJiangFeng
* @updateTime 2019/12/11 16:39
* @throws
*/
@RequestMapping("/getTest1")
@ResponseBody
public String getTest1(){
String url="http://127.0.0.1:8082/demoTest2";
// HttpHeaders headers = new HttpHeaders();
// headers.setContentType(MediaType.APPLICATION_JSON);
// headers.set("phone", "123456");
//傳參數可以使用map,也可以使用user
//如果使用map,下面postForEntity要改成Map類型;postForEntity(url, map,String.class);
// Map<String,Object> map=new HashMap<String, Object>();
// map.put("name","樊江鋒");
// map.put("email","[email protected]");
// 如果使用user對象,要改成user對象 postForEntity(url, user,String.class);
// User user=new User();
// user.setName("袁夢陽");
// user.setEmail("[email protected]");
//當所有傳遞的數據類型爲複雜數據類型,並且需要接受的參數不再實體類中進行映射。MultiValueMap來進行傳遞。
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("name", "袁夢陽");
params.add("email", "[email protected]");
RestTemplate restTemplate = new RestTemplate();
//HttpEntity用來裝載請求頭或者MultiValueMap集合(參數)
// HttpEntity httpEntity = new HttpEntity(params,headers);
ResponseEntity<String> request = restTemplate.postForEntity(url,params,String.class);
System.out.println(request.getBody());
return request.getBody();
}
提供者接收並響應請求
/**
* @title
* @description post請求
* @author FanJiangFeng
* @updateTime 2019/12/11 16:39
* @throws
* //post請求 如果client參數是MultiValueMap傳遞,需要去掉consumes = MediaType.APPLICATION_JSON_VALUE
*/
@RequestMapping(value = "demoTest2",produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody //如果client參數是對象,則括號中爲 @RequestBody User user 如果是MultiValueMap,則括號中爲 @RequestParam String name,@RequestParam String email
public String demoTest2(@RequestParam String name,@RequestParam String email){
// System.out.println(user.getName());
// System.out.println(user.getEmail());
System.out.println(name);
System.out.println(email);
return "success";
}
三,綜合示例
可以參考下方的RestTemplate
的例子發送遠程請求。
發送方
/**
* @Description 發送遠程請求,進入投標保險首頁
* @Date 2019/12/16 15:57
* @return
* @Author FanJiangFeng
* @Version1.0
* @History
*/
public String sendPostReq(String requestData) throws IOException {
RestTemplate restTemplate = new RestTemplate();
String url = "http://127.0.0.1:7000/tbbx/insurance/enter";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add("requestData",requestData);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<String> response = restTemplate.postForEntity( url, request , String.class );
String responseData = response.getBody();
return responseData;
}
接收方
/**
* @title
* @description 招採平臺進入本平臺入口
* @author FanJiangFeng
* @updateTime 2019/12/10 15:59
* @throws
*/
@RequestMapping(value = "/enter",produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String test(@RequestParam String requestData) throws Exception {
JSONObject jsonObject = JSONObject.parseObject(requestData);
String APPID=(String) jsonObject.get("APPID");
String encryptParam=(String) jsonObject.get("encryptParam");
String signature=(String) jsonObject.get("signature");
//返回json串數據
JSONObject map=new JSONObject();
map.put("status",1000);
map.put("message","連接成功");
String jsonString = JSONObject.toJSONString(map);
return jsonString;
}
順利請求並反饋信息流程成功!