SpringCloud 마이크로 서비스의 나머지 를 자세히 설명 합 니 다.

1.RestTemplate 가 무엇 입 니까?
RestTemplate 는 HTTP 클 라 이언 트 로 Spring Cloud 의 서비스 호출 자 에서 사용 하면 HTTP 인 터 페 이 스 를 편리 하 게 호출 할 수 있 고 GET,POST,PUT,DELETE 등 방법 을 지원 합 니 다.
두,네 가지 요청 방식
일단 빈 개체 주입.

@Configuration
public class MyConfig {
  @Bean
  public RestTemplate restTemplate(){
      return new RestTemplate();
  }
}
2.1 GET 요청
  • 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);
        }
    }
    
    2.2 POST 요청
  • 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 요청
    
    @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 에 관 한 더 많은 내용 은 저희 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 읽 어 주시 기 바 랍 니 다.앞으로 많은 응원 부탁드립니다!

    좋은 웹페이지 즐겨찾기