1.RestTemplate
RestTemplate的底層是通過HttpURLConnection實現的(注意:java.net.HttpURLConnection.setRequestMethod 不支持PATCH方法,無法將請求發送出去)。參考文章
訪問已經創建好的rest風格後端。這裏使用SpringBoot學習8.0-創建REST風格站點介紹的後端代碼。
2.RestTemplate使用
用RestTemplate調用一下方法:
- GET請求:postForObject()
- POST請求:getForObject()、getForEntity()
- PUT請求:put()
- PATCH請求:不支持
- DELETE請求:delete()
舉例說明:
其中http://localhost:9001/是啓動的用戶微服務ip和端口。
package com.zyf.appserver.user;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
@Controller
@ResponseBody // 請求結果默認轉換爲json
public class UserController {
// rest請求模板
RestTemplate restTemplate = new RestTemplate();
@RequestMapping("/insert")
public User insert(@RequestBody User user) {
HttpHeaders headers = new HttpHeaders();
HttpEntity<User> reqeust = new HttpEntity<>(user, headers);
// post請求
return restTemplate.postForObject("http://localhost:9001/userRestController/user", reqeust, User.class);
}
@RequestMapping(value = "/getUser/{id}")
public User getUser(@PathVariable int id) {
// get請求
return restTemplate.getForObject("http://localhost:9001/userRestController/user/{id}", User.class, id);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@RequestMapping("/getUsers/{id}/{name}")
public List<User> getUsers(@PathVariable String id, @PathVariable String name) {
Map<String, Object> param = new HashMap<String, Object>();
param.put("id", id);
param.put("name", name);
// get請求
ResponseEntity<List> responseEntity = restTemplate
.getForEntity("http://localhost:9001/userRestController" + "/user/{id}/{name}", List.class, param);
List<User> users = responseEntity.getBody();
return users;
}
@RequestMapping("/update/{id}") // 更新全部屬性
public User update(@PathVariable int id, @RequestBody User user) {
HttpHeaders headers = new HttpHeaders();
HttpEntity<User> reqeust = new HttpEntity<>(user, headers);
// put請求
restTemplate.put("http://localhost:9001/userRestController/user/{id}", reqeust, id);
return user;
}
@RequestMapping("/updatepatch/{id}/{name}/{note}") // 更新部分屬性
public User updatepatch(@PathVariable int id, @PathVariable String name, @PathVariable String note) {
Map<String, Object> param = new HashMap<String, Object>();
param.put("id", id);
param.put("name", name);
param.put("note", note);
HttpHeaders headers = new HttpHeaders();
HttpEntity<Map<String, Object>> reqeust = new HttpEntity<>(param, headers);
// patch請求
// RestTemplate的底層是通過HttpURLConnection實現的(注意:java.net.HttpURLConnection.setRequestMethod
// 不支持PATCH方法,無法將請求發送出去)。
return restTemplate.patchForObject("http://localhost:9001/userRestController/user/{id}/{name}/{note}", reqeust,
User.class, param);
}
@RequestMapping("/delete")
public User delete(int id) {
// delete請求
restTemplate.delete("http://localhost:9001/userRestController/user/{id}", id);
return new User(1, "吉姆格林", 1, "rest風格,刪除");
}
}
github:https://github.com/zhangyangfei/spring-cloud-learn.git 中的cloud-parent工程。