Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Rest and request method in SpringCloud micro-service

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge of this article "Rest and request method in Spring Cloud Micro Service", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this article "what is the Rest and request method in Spring Cloud Micro Service".

What is RestTemplate?

RestTemplate is a HTTP client, we can easily call HTTP interface in Spring Cloud service caller, and support GET, POST, PUT, DELETE and other methods.

II. Four ways of request

First inject the Bean object

@ Configurationpublic class MyConfig {@ Bean public RestTemplate restTemplate () {return new RestTemplate ();}} 2.1 GET request

GetForObject

@ GetMapping ("get/ {id}") public CommonResult getUser (@ PathVariable Long id) {CommonResult commonResult = restTemplate.getForObject (Url + "/ user/ {1}", CommonResult.class, id); return commonResult}

GetForEntity

@ GetMapping ("/ get/ {sex}") public CommonResult getUser (@ PathVariable String sex) {ResponseEntity entity = restTemplate.getForEntity (Url + "/ user/ {female}, CommonResult.class, sex); if (entity.getStatusCode (). Is2xxSuccessful ()) {return entity.getBody ();} else {return new CommonResult (" operation failed ", 500);}} 2.2POST request

PostForObject

@ PostMapping ("/ add") public CommonResult add (@ RequestBody User user) {CommonResult commonResult = restTemplate.postForObject (Url + "/ user/add", user, CommonResult.class); return commonResult;}

PostForEntity

@ PostMapping ("/ add") public CommonResult add (@ RequestBody User user) {CommonResult commonResult = restTemplate.postForEntity (Url + "/ user/add", user, CommonResult.class) return commonResult.getBody ();} 2.3 PUT request @ PutMapping ("/ update") public CommonResult update (@ RequestBody User user) {restTemplate.put (Url + "/ user/update", user); return new CommonResult ("Operation successful", 200) } 2.4 DELETE request @ DeleteMapping ("/ delete/ {id}") public CommonResult delete (@ PathVariable Long id) {restTemplate.delete (Url + "/ user/delete/" + id, null); return new CommonResult ("Operation successful", 200) } the above is the content of this article on "what is the Rest and request method in Spring Cloud Micro Service". I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report