Web | Springboot | RestTemplate

RestTemplate之Http Get请求

1. RestTemplate. getForobject

方法可以获取对象

方法说明

/**
* url:请求地址
* responseType:返回的数据封装的类型,指定class类型,可以是Map,会返回 Map<String, Object>
* uriVariables:封装的请求参数
*/
public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables)
public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables)
public <T> T getForObject(URI url, Class<T> responseType)

样例程序

//http://localhost:8080/getforobject
@GetMapping"/getForobject"public object getForobject(){
//远程访问的Url 自定义的一个User类 
String url=http://localhost:8080/adduser/100000/fencaibc/请求入参
//封装的请求参数,这里未填写
Map<String, Long> paramMap = new HashMap<>();
User result= restTemplate getForobject(url, User.class, paramMap);
// Map<String, Object> result restTemplate getForobject(url, Map.class, paramMap);
return result;

2. RestTemplate. getForEntity

方法不仅可以获取对象,还可以获取Http状态码,请求头等详细信息,返回的是ResponseEntity<T>对象里面封装的除了响应体外还有响应行的一些信息。

方法说明

public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables)
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables)
public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType)

样例程序

// 请求 http://localhost:8080/getforentity
@GetMapping"/getForEntity"public Map<String, Object> getForEntity(){
//远程访问的Url UserDTO
String url =http://lopalhost:8080/adduser/100000/ab",
//封装请求参数,这里是空的入参
Map<String,Long> paramMap = new HashMap<>();
// ResponseEntity包装返回结果
ResponseEntity<HashMap> responseEntity = restTemplate.getForEntity(url, HashMap.class, paramMap)//返回状态码包装类
Httpstatus statuscode= responseentity.getstatusCode()//返回状态码
int statusCode Value = responseEntity.getStatusCodeValue()//Http返回头
Httpheaders headers = responseentity.getheaders()/返回对应的请求结果
return responseEntity.getBody()

RestTemplate之HttpPost请求

Post方法的 MultiValue Map既支持基本类型分开传参,也支持实体传参。类似下面的形式。

1. postForObject()

RestTemplate的Post方法与Get方法的区别是Post方法传参如果是Map类型封装的则必须是 MultivalueMap类型。

基本类型传参和实体传参

//http://localhost:8080/postforobject1
@GetMapping"/postForobject1"public User postForobject(){
}
//远程访问的Ur1 
String url = "http://localhost:8080/adduser1"
//Post方法必须使用 MultiVa1ueMap传参, 使用User传参也可以
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
paramMap.add("userId",1);
paramMap.add("userName""zhangsan");
//返回对象
User user= restTemplate postForobject(url, paramMap, User.class);
return user;

如果服务端是@RequestBody传参

需要使用HttpEntity来封装传参,并指定请求头的信息。

//http://logalhost:8080/postforobject2
@GetMapping("/postForobject2")
public User postForobject20(){
	//申明一个请求头
	HttpHeaders headers = new HttpHeaders()//设置 请求行参数application/json
	headers. setContentType( MediaType APPLICATION_JSON );
	//远程访问的Url
	Stringurl = http://localhost:8080/adduser3
	/**
	此处使用Mu1tiValueMap会报错
	MultivalueMap<String, Object> paramMap = new LinkedMultivalueMap<>();
	paramMap.add("userId",1000L);
	paramMap.add("userName",fencaibc");
	*/
	//此处可以使用 HashMap代替,但是会有警告
	User user = new User();
	user.setUserId(1);
	user.setUserName("zhangsan")//使用 HttpEntity形式包装传参,包括请求头和实体数据对象(请求体)
	HttpEntity<user> entityParam = new HttpEntity<user>(user, headers);
	//注意参数的位置顺序和get的不同
	User result = restTemplate.postForobject(url,entityParam,User.class);
	return result;
}

2. postForEntity()

返回值需要用ResponseEntity来封装一下,ResponseEntity在get请求中说过,服务端是@RequestBody传参不再介绍,一样的。

//http://localhost:8080/postforentity1
@GetMapping"/postForEntity1"public User postForEntity(){
	//远程访问的Url
	String url = http://localhost:8080/adduser1";
	MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>():
	paramMap.add("userId"1);
	paramMap.add("userName""zhangsan");
	ResponseEntity<User> userResponseEntity = restTemplate.postForEntity(url, paramMap, User.class);
	Httpstatus statusCode = userResponseEntity.getStatusCode();
	int statusCodeValue = userResponseEntity.getStatusCodeValue();
	Httpheaders headers = userdtoresponseEntity.getheaders();
	return userResponseEntity.getBody();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值