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工程。