Start a spring Cloud project - use RestTemplate to implement calls between Module and Module

Use of RestTemplate

  • What is RestTemplate?
    RestTemplate is a REST-based service component provided by the Spring framework. The bottom layer encapsulates HTTP requests and responses. It provides many methods to access REST services, which can simplify code development.
  • How to use RestTemplate
    1. Create a Maven project, pom.xml (here I build it directly in the parent project built earlier, only the springboot dependency is required, and the RestTemplate method is already included) and
    name it: resttemplate
    2. Put the previous step Copy the entity class created in
    3. Create a new startup class in the java folder, inject the RestTemplate instance into the ioc, and create a new controller named RestHandler
    Insert image description here

RestTemplateApplication code is as follows:

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class RestTemplateApplication {
    
    
    public static void main(String[] args){
    
    
        SpringApplication.run(RestTemplateApplication.class);
    }


    //在这里注入RestTemplate实例,这样就可以使用RestTemplate
    @Bean   //把RestTemplate 添加到ioc容器
    public RestTemplate restTemplate(){
    
    
        return new RestTemplate();
    }
}

RestHandler code content is as follows:

Here the RestTemplate method is used to implement calls between modules

There are two methods in total:

  • The type returned by the .getForEntity() method is a generic wrapped by ResponseEntity, so if you want to customize the returned type, you need to call the .getBody() method, otherwise you can only wrap the returned type into ResponseEntity<>
    Insert image description here
  • The .getForObject() method returns a generic type, so there is no need to call other methods. It directly returns whatever type is returned in the defined method.
    Insert image description here
package com.southwind.controller;

import com.southwind.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;

@RestController
@RequestMapping("/rest")
public class RestHandler {
    
    
    @Autowired
    private RestTemplate restTemplate;

    //在这里通过RestTemplate的方法调用 eruekaclient 模块的方法
    //第一种方法,使用getForEntity,返回的是 ResponseEntity<T>,所以需要通过调用.getBody()方法
    @GetMapping("/findAll")
    public Collection<Student> findAll(){
    
    
        return restTemplate.getForEntity("http://localhost:8010/student/findAll",Collection.class).getBody();
    }
    //第二种方法,使用getForObject ,因为此方法返回值就是泛型 ,所以直接返回即可
    @GetMapping("/findAll2")
    public Collection<Student> findAll2(){
    
    
        return restTemplate.getForObject("http://localhost:8010/student/findAll",Collection.class);
    }
    @GetMapping("/findById/{id}")
    public Student  findById(@PathVariable("id") long id){
    
    
        return restTemplate.getForEntity("http://localhost:8010/student//findById/{id}",Student.class,id).getBody();
    }

    @GetMapping("/findById2/{id}")
    public Student findById2(@PathVariable("id") long id){
    
    
        return restTemplate.getForObject("http://localhost:8010/student/findById/{id}",Student.class,id);
    }

    @PostMapping("/save")
    public void save(@RequestBody Student student){
    
    
        restTemplate.postForEntity("http://localhost:8010/student/save",student,null).getBody();
    }

    @PostMapping("/save2")
    public void save2(@RequestBody Student student){
    
    
        restTemplate.postForObject("http://localhost:8010/student/save",student,void.class);
    }
    @PutMapping("/update")
    public void update(@RequestBody Student student){
    
    
        restTemplate.put("http://localhost:8010/student/update",student);

    }
    @DeleteMapping("deleteById/{id}")
    public void deleteById(@PathVariable("id") long id){
    
    
        restTemplate.delete("http://localhost:8010/student/deleteById/{id}",id);
    }
}

Student code is as follows:

package com.southwind.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor                 //无参构造
@AllArgsConstructor                //有参构造
public class Student {
    
    
    private long id;
    private String name;
    private int age;

}

Completed, start the eurekaclient project (the service provider project created in the second part), start the resttemplate project, and use postman to test additions, deletions, modifications and checks.

Here, by accessing the controller method in resttemplate, you can operate the data in the eruekaclient project and implement calls between modules.

Inquire:
Insert image description here

Add:
Insert image description here
Modify:
Insert image description here
Delete:
Insert image description here

Guess you like

Origin blog.csdn.net/chenmaolin928/article/details/109133209