java resttemplate_RestTemplate的三种用法介绍(代码)

本篇文章给大家带来的内容是关于RestTemplate的三种使用方式介绍(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

准备

服务端我是用的是一个普通的API@RestController

public class ServerController {

@GetMapping("/msg")

public String msg(){

return "this is product' msg";

}

}

第一种方式

直接使用restTemplate,url写死@Slf4j

@RestController

public class ClientController {

@GetMapping("/getProductMsg")

public String getProductMsg(){

// 1、第一种方式(直接使用restTemplate,url写死)

RestTemplate restTemplate = new RestTemplate();

String response = restTemplate.getForObject("http://localhost:9082/msg",String.class);

log.info("response={}",response);

return response;

}

}

第二种方式第二种方式(利用loadBalancerClient通过应用名获取url,然后再使用restTemplate)@Slf4j

@RestController

public class ClientController {

@Autowired

private LoadBalancerClient loadBalancerClient;

@GetMapping("/getProductMsg")

public String getProductMsg(){

//2、第二种方式(利用loadBalancerClient通过应用名获取url,然后再使用restTemplate)

ServiceInstance serviceInstance = loadBalancerClient.choose("PRODUCT");

String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort()) + "/msg";

RestTemplate restTemplate = new RestTemplate();

String response = restTemplate.getForObject(url,String.class);

log.info("response={}",response);

return response;

}

}

第三种方式第三种方式(利用@LoadBalanced,可再restTemplate里使用应用名字)@Component

public class RestTemplateConfig {

@Bean

@LoadBalanced

public RestTemplate restTemplate(){

return new RestTemplate();

}

}@Slf4j

@RestController

public class ClientController {

@Autowired

private RestTemplate restTemplate;

@GetMapping("/getProductMsg")

public String getProductMsg(){

//3、第三种方式(利用@LoadBalanced,可再restTemplate里使用应用名字)

String response = restTemplate.getForObject("http://PRODUCT/msg",String.class);

log.info("response={}",response);

return response;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RestTemplate 是一个用于发送 HTTP 请求和处理响应的 Java 类,它是 Spring Framework 的一部分。通过 RestTemplate,您可以轻松地与 RESTful Web 服务进行交互。 您可以使用 RestTemplate 发送 GET、POST、PUT、DELETE 等各种类型的请求,并且可以在请求中添加参数、请求头和请求体。它还提供了许多便捷的方法来处理响应,包括将响应转换为对象、获取响应的状态码、获取响应头等。 以下是一个使用 RestTemplate 发送 GET 请求的示例: ```java RestTemplate restTemplate = new RestTemplate(); String url = "https://api.example.com/users/{userId}"; String userId = "123"; User user = restTemplate.getForObject(url, User.class, userId); ``` 在上面的示例中,我们创建了一个 RestTemplate 实例,并且指定了要发送的 GET 请求的 URL。我们还使用占位符 `{userId}` 指定了要替换的路径参数。然后,我们调用了 `getForObject` 方法,并指定了响应的类型(在此示例中为 `User.class`)和替换占位符的值(在此示例中为 `userId`)。最后,我们将响应转换为 User 对象。 除了发送 GET 请求,您还可以使用 RestTemplate 发送 POST、PUT 和 DELETE 请求,只需更改方法调用即可。 RestTemplate 提供了许多其他功能,例如添加请求头、处理异常和处理文件上传。您可以根据需要进一步探索 RestTemplate 的功能。 注意:自从 Spring Framework 5.0 版本开始,推荐使用 WebClient 替代 RestTemplate,因为 WebClient 提供了更好的性能和更多的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值