本文介(jie)紹(shao)restTemplate基礎用法。
Java中get和post的用法請參(can)考(kao)://mp.weixin.qq.com/s/mC0D1nuCqIori5bWtLorWQ
1 提供(gong)get/post接口
1.1 Controller
@RestController
@RequestMapping("/homepage")
public class MyController {
?
@Autowired
MyService myService;
?
// 提(ti)供get接口
@GetMapping("/provideGet")
public Map<String, String> provideGet(){
return myService.provideGet();
}
?
// 提供post接口(kou)
@PostMapping("/providePost")
public Map<String, Object> providePost(@RequestParam("number") int number, @RequestParam("name") String name) {
return myService.providePost(number, name);
}
?
// 提供(gong)map參數(shu)的post接(jie)口
@PostMapping("/providePostByMap")
public Map<String, Object> providePostByMap(@RequestParam Map<String, Object> map) {
return myService.providePostByMap(map);
}
?
// 調用get接(jie)口
@GetMapping("/useGet")
public Map<String, Object> useGet() {
return myService.useGet();
}
}
1.2 Service
@Service
@EnableScheduling
public class MyService {
?
public Map<String, String> provideGet() {
Map<String, String> res = new HashMap<>();
res.put("number", "3");
res.put("name", "張三get");
System.out.println("provideGet res:" + res + "\n");
return res;
}
?
public Map<String, Object> providePost(int number, String name) {
Map<String, Object> res = new HashMap<>();
res.put("number", number);
res.put("name", name);
?
return res;
}
?
public Map<String, Object> providePostByMap(Map<String, Object> map) {
int number = map.get("number") == null ? 0 : Integer.parseInt((String) map.get("number"));
String name = map.get("name") == null ? "" : (String) map.get("name");
Map<String, Object> res = new HashMap<>();
res.put("number", number);
res.put("name", name);
?
System.out.println("providePostByMap res:" + res + "\n");
return res;
}
}
2 調(diao)用get/post接口
使用restTemplate調用get/post接口。
-
getForObject():返回(hui)值(zhi)是HTTP協(xie)議的(de)響應體 -
getForEntity():返(fan)回的(de)是(shi)ResponseEntity,ResponseEntity是對HTTP響應的封裝,除了包(bao)含響應體,還包(bao)含HTTP狀態碼、contentType、contentLength、Header等信息
2.1 Controller
@RestController
@RequestMapping("/homepage")
public class MyController {
@Autowired
MyService myService;
?
// 調用get接口
@GetMapping("/useGet")
public Map<String, Object> useGet() {
return myService.useGet();
}
?
// 調用get接口(kou)驗證賬號密碼
@GetMapping("/useGetByPsw")
public Map<String, Object> useGetByPsw() {
return myService.useGetByPsw();
}
?
// 調(diao)用post接口
@PostMapping("/usePost")
public Map<String, Object> usePost() {
return myService.usePost();
}
}
2.2 Service
@Service
@EnableScheduling
public class MyService {
@Resource
private RestTemplate restTemplate;
?
String getURL = "//localhost:8081/homepage/provideGet";
String postURL = "//localhost:8081/homepage/providePostByMap";
?
public Map<String, Object> useGet() {
// getForObject返回(hui)值是HTTP協議的響應(ying)體
String strObject1 = restTemplate.getForObject(getURL, String.class); //無參
JSONObject jsonObject1 = JSONObject.parseObject(strObject1);
?
MultiValueMap<String, String> sendData = new LinkedMultiValueMap<>();
sendData.add("number", "3");
sendData.add("name", "張三post");
String strObject2 = restTemplate.getForObject(getURL, String.class, sendData); // 帶(dai)參
JSONObject jsonObject2 = JSONObject.parseObject(strObject2);
?
// getForEntity返回的是(shi)ResponseEntity,是(shi)對HTTP響應的封裝
ResponseEntity<ResponseResult> responseData = restTemplate.getForEntity(getURL, ResponseResult.class);
Map<String, Object> returnData = new HashMap<>();
returnData.put("StatusCode:", responseData.getStatusCode());
returnData.put("Body:", responseData.getBody());
?
System.out.println("useGet jsonObject1:" + jsonObject1 + "\n");
System.out.println("useGet jsonObject2:" + jsonObject2 + "\n");
System.out.println("useGet responseData:" + responseData + "\n");
System.out.println("useGet returnData:" + returnData + "\n");
return returnData;
}
?
public Map<String, Object> useGetByPsw() {
?
RestTemplateBuilder builder = new RestTemplateBuilder();
RestTemplate restTemplate = builder.basicAuthentication("username", "password").build();
?
// getForEntity返回的是ResponseEntity,是對HTTP響(xiang)應的封裝
ResponseEntity<ResponseResult> responseData = restTemplate.getForEntity(getURL, ResponseResult.class);
Map<String, Object> returnData = new HashMap<>();
returnData.put("StatusCode:", responseData.getStatusCode());
returnData.put("Body:", responseData.getBody());
?
System.out.println("useGetByPsw returnData:" + responseData + "\n");
System.out.println("useGetByPsw returnData:" + returnData + "\n");
return returnData;
}
?
public Map<String, Object> usePost() {
//RestTemplate在postForObject時,用MultiValueMap,不(bu)可使(shi)用HashMap。
MultiValueMap<String, String> sendData = new LinkedMultiValueMap<>();
sendData.add("number", "3");
sendData.add("name", "張(zhang)三post");
?
// getForObject返回值是(shi)HTTP協(xie)議(yi)的響應體(ti)
String strObject = restTemplate.postForObject(postURL, sendData, String.class);
JSONObject jsonObject = JSONObject.parseObject(strObject);
?
// getForEntity返回(hui)的是ResponseEntity,是對(dui)HTTP響應的封裝
ResponseEntity<ResponseResult> responseData = restTemplate.postForEntity(postURL, sendData, ResponseResult.class);
Map<String, Object> returnData = new HashMap<>();
returnData.put("StatusCode:", responseData.getStatusCode());
returnData.put("Body:", responseData.getBody());
?
System.out.println("usePost jsonObject:" + jsonObject + "\n");
System.out.println("usePost responseData:" + responseData + "\n");
System.out.println("usePost returnData:" + returnData + "\n");
return returnData;
}
}