您好,登錄后才能下訂單哦!
這篇“SpringCloud微服務(wù)中的Rest及請(qǐng)求方式是什么”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“SpringCloud微服務(wù)中的Rest及請(qǐng)求方式是什么”文章吧。
RestTemplate 是一個(gè)HTTP客戶(hù)端,在Spring Cloud的服務(wù)調(diào)用方使用它我們可以方便的調(diào)用HTTP接口,支持GET、POST、PUT、DELETE等方法。
首先注入Bean對(duì)象
@Configuration
public class MyConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
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<CommonResult> entity
= restTemplate.getForEntity(Url + "/user/{女}", CommonResult.class, sex);
if (entity.getStatusCode().is2xxSuccessful()) {
return entity.getBody();
} else {
return new CommonResult("操作失敗", 500);
}
}
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();
}
@PutMapping("/update")
public CommonResult update(@RequestBody User user) {
restTemplate.put(Url + "/user/update", user);
return new CommonResult("操作成功",200);
}
@DeleteMapping("/delete/{id}")
public CommonResult delete(@PathVariable Long id) {
restTemplate.delete(Url + "/user/delete/" + id, null);
return new CommonResult("操作成功",200);
}
以上就是關(guān)于“SpringCloud微服務(wù)中的Rest及請(qǐng)求方式是什么”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。