RestTemplate远程接口调用

spring rest mvc使用RestTemplate远程接口调用


主要代码如下: 

import java.util.HashMap; 
import java.util.Map; 

import org.springframework.web.client.RestTemplate; 

/** 
* RestTemplate提供了一系列调用spring mvc rest(或者说 spring rest webservice)接口 
* 包括 get/post/delete/put/ 

*/ 
public class Resttemplate { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
RestTemplate restTemplate = new RestTemplate();   

//get方式*********************************************************************************************************** 

// //参数直接放在URL中 
// String message =restTemplate.getForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80",String.class ); 
// 
// 
// //参数使用MAP传递 
// Map<String ,Object> urlVariables = new HashMap<String,Object>(); 
// urlVariables.put("name", "zhaoshijie"); 
// urlVariables.put("id", 80); 
// String message2 =restTemplate.getForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate",String.class, urlVariables); 



//delete方式*********************************************************************************************************** 

//delete方法(注意:delete方法没有返回值,说明,id=0这个参数在服务器端可以不定义该参数,直接使用request获取) 
//restTemplate.delete("http://localhost:8080/yongbarservice/appstore/appgoods/deleteranking?id=0"); 




//post方式*********************************************************************************************************** 
//使用MAP传递参数 
// Map<String ,Object> urlVariables = new HashMap<String,Object>(); 
// urlVariables.put("name", "zhaoshijie"); 
// urlVariables.put("id", 80); 
// String message3 =restTemplate.postForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate",null,String.class, urlVariables); 

//直接使用URL传递参数 
// String message =restTemplate.postForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80",null,String.class ); 



//put方式*********************************************************************************************************** 
//注意:delete方法没有返回值,说明,id=0这个参数在服务器端可以不定义该参数,直接使用request获取 
restTemplate.put("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80",null); 



// System.out.println(message); 
// System.out.println(message2); 
// System.out.println(message3); 



 


### 使用 RestTemplate 进行远程调用路径设置 为了通过 `RestTemplate` 实现远程HTTP请求并指定访问路径,通常会经历几个重要的环节。首先,在Spring应用中注册一个 `RestTemplate` 到Spring容器内[^1]: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class RemoteCallConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } } ``` 接着,创建 `RestTemplate` 对象之后就可以利用它来进行GET、POST等各种类型的HTTP请求操作[^2]。 对于具体的远程调用路径配置而言,当执行一次简单的GET请求时,可以通过传递完整的URL字符串给相应的方法来完成。下面是一个使用 `RestTemplate` 发起带有具体路径参数的GET请求的例子: ```java // 假设这是在一个服务类里边实现的功能逻辑部分 @Autowired private RestTemplate restTemplate; public User queryUserInfo(Long userId){ // 构建目标地址,其中包含了动态变化的部分即用户的ID String url = "http://example.com/api/users/{id}"; // 调用exchange方法发起带路径变量的GET请求,并接收响应体转换成User对象 ResponseEntity<User> responseEntity = restTemplate.getForEntity(url, User.class, userId); // 返回获取到的结果实体中的body数据 return responseEntity.getBody(); } ``` 上述代码片段展示了如何构建包含路径参数的API URL以及怎样发送GET请求并处理返回的数据。这里使用的 `{id}` 是占位符语法的一部分,允许开发者在实际发出请求前插入特定值作为该位置上的内容。 另外一种方式是采用Feign客户端的方式简化声明式的Web服务客户端开发过程。这种方式下不需要手动管理 `RestTemplate` 或者直接编写HTTP请求细节;而是定义接口并通过注解描述所需的服务端点及其行为即可[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值