In the actual project, it is inevitable to interact with other systems, which will involve interface call. If it is good for your own system, you can directly call it in this project by means of jar package reference.

If it is from another system, it needs to be called through a remote interface, and remote interface calls are usually made in two ways

  1. One is the WebService interface,
  2. The other is the REST interface (where they give us an HTTP link)

This article shows how to invoke a remote service through a REST interface.

Before SpringBoot, people would think of using HttpClient to call, and yes, HttpClient is a good choice. But SpringBoot encapsulates a much better call, RestTemplate!

Establish the following project directory structure:

The key classes are listed one by one:
@RestController
@RequestMapping("demo")

public class DemoController {

    @GetMapping("getUser")
    public void getUser(@RequestParam("name")String name){
        // You need to call a third-party service remotely to get the user object
    }

    @GetMapping("saveUser")
    public void getUser(a){
        UserDto userDto = new UserDto();
        userDto.setName("resttemplate");
        userDto.setAge(17);
        userDto.setBirthday(new Date());
        // You need to call a third-party service remotely to send the user object to the third party}}Copy the code
@RestController
@RequestMapping("api")

// This class is used to simulate third-party services
public class ApiController {

    // Simulate the data
    @GetMapping("get")
    public UserDto get(@RequestParam("username")String username){
        System.out.println("Select username from database ["+username+"】");
        UserDto userDto = new UserDto();
        userDto.setName("springboot");
        userDto.setAge(18);
        userDto.setBirthday(new Date());

        return userDto;
    }

    // Analog received data saved to the database
    @GetMapping("save")
    public UserDto save(@RequestBody UserDto userDto) throws JsonProcessingException {
        System.out.println(new ObjectMapper().writeValueAsString(userDto));
        //TODO:Save to database
        returnuserDto; }}Copy the code
@Data

public class UserDto {

    private String name;
    private Integer age;
    private Date birthday;
}
Copy the code

To start the project, let’s use Postman to make sure the following two services are ok.

Get: localhost:8080/demo/get POST: localhost:8080/demo/saveCopy the code

post:localhost:8080/demo/save

To simplify the simulation, we will also deploy third-party services in this project, namely localhost:8080/demo/get and localhost:8080/demo/save.

Now we need to use the remote call in ApiController to send the request. I talked about RestTemplate earlier.

We declare a RestTemplate in the Application

@Bean

public RestTemplate restTemplate(a){
    return new RestTemplate();
}
Copy the code

Then inject it in the DemoController with @Resource

Here we call http://localhost:8080/demo/getUser? by postman Name = 123 to see see.

Here is the console output. The first sentence is the output from ApiController and the second sentence is the output from the parent call, indicating that we have called another REST service through demoController

After sending a POST request:

Here is the output from the console, the first sentence is also the output from the apiController, and the second sentence is the output from the superior call, again as expected.

The actual production instance will also involve timeouts, and if the third party response is too slow, the wait will be stopped.

Yes. Here we can do the same thing with RestTemplate, didn’t we declare a RestTemplate in the Application? , simply replace the following code.

@Bean
public SimpleClientHttpRequestFactory httpClientFactory(a) {
    SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory();
    httpRequestFactory.setReadTimeout(1000*5);// Read timeout
    httpRequestFactory.setConnectTimeout(1000*5);// Link timeout

    return httpRequestFactory;
}

@Bean
public RestTemplate restTemplate(SimpleClientHttpRequestFactory httpClientFactory) {
    RestTemplate restTemplate = new RestTemplate(httpClientFactory);
    return restTemplate;
}
Copy the code

More Java original reading: Javawu.com