SpringCloud 마이크로 서비스의 나머지 를 자세히 설명 합 니 다.
2567 단어 SpringCloud마이크로 서비스Rest
RestTemplate 는 HTTP 클 라 이언 트 로 Spring Cloud 의 서비스 호출 자 에서 사용 하면 HTTP 인 터 페 이 스 를 편리 하 게 호출 할 수 있 고 GET,POST,PUT,DELETE 등 방법 을 지원 합 니 다.
두,네 가지 요청 방식
일단 빈 개체 주입.
@Configuration
public class MyConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
2.1 GET 요청
@GetMapping("get/{id}")
public CommonResult getUser(@PathVariable Long id) {
CommonResult commonResult
= restTemplate.getForObject(Url + "/user/{1}", CommonResult.class, id);
return commonResult
}
@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);
}
}
2.2 POST 요청
@PostMapping("/add")
public CommonResult add(@RequestBody User user) {
CommonResult commonResult
= restTemplate.postForObject(Url + "/user/add", user, CommonResult.class);
return commonResult;
}
@PostMapping("/add")
public CommonResult add(@RequestBody User user) {
CommonResult commonResult
= restTemplate.postForEntity(Url + "/user/add", user, CommonResult.class)
return commonResult.getBody();
}
2.3 PUT 요청
@PutMapping("/update")
public CommonResult update(@RequestBody User user) {
restTemplate.put(Url + "/user/update", user);
return new CommonResult(" ",200);
}
2.4 DELETE 요청
@DeleteMapping("/delete/{id}")
public CommonResult delete(@PathVariable Long id) {
restTemplate.delete(Url + "/user/delete/" + id, null);
return new CommonResult(" ",200);
}
스프링 클 라 우 드 마이크로 서비스의 Rest 에 대한 상세 한 설명 은 여기까지 입 니 다.스프링 클 라 우 드 Rest 에 관 한 더 많은 내용 은 저희 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 읽 어 주시 기 바 랍 니 다.앞으로 많은 응원 부탁드립니다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Spring Cloud] LoadBancer-01discoveryservice-eureka 라는 프로젝트를 생성한다 3개 항목을 추가한다. 1. DiscoveryserviceEurekaApplication.java 생성된 -application에 @EnableEu...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.