SpringBoot学习8.1-RestTemplate请求rest风格后端

17 篇文章 0 订阅
10 篇文章 0 订阅

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值