In chapter 9, we discussed implementing REST endpoints. REST services are a common way to implement the communication between two system components. The client of a web app can call the backend, and so can another backend component. In a backend solution composed of multiple services, these components need to “speak” to exchange data, so when you implement such a service using Spring, you need to know how to call a REST endpoint exposed by another service:

Untitled

In this chapter, you’ll learn a way to call REST endpoints from a Spring app: OpenFeign —A tool offered by the Spring Cloud project. I recommend developers use this feature in new apps for consuming REST endpoints. As you’ll learn, OpenFeign offers a simple syntax and makes calling a REST endpoint from a Spring app straightforward.

Suppose you implement an app that allows users to make payments. To make a payment, you need to call an endpoint of another system.

The project consists of:

Details the scenario showing the request and response details:

Untitled

Untitled

With this first project, we implement the payment service app. We need to add to the pom.xml file the web dependency, as presented in the next code snippet:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

We’ll model the payment with the Payment class, as presented in the next code snippet:

public class Payment {
  private String id;
  private double amount;

 // Omitted getters and setters
}

The endpoint’s implementation in the controller class:

@RestController
public class PaymentsController {

  private static Logger logger =                            ❶
    Logger.getLogger(PaymentsController.class.getName());

  @PostMapping("/payment")                                  ❷
  public ResponseEntity<Payment> createPayment(
      @RequestHeader String requestId,                      ❸
      @RequestBody Payment payment                          ❸
  ) {
    logger.info("Received request with ID " + requestId +
        " ;Payment Amount: " + payment.getAmount());

    payment.setId(UUID.randomUUID().toString());            ❹

    return ResponseEntity                                   ❺
        .status(HttpStatus.OK)
        .header("requestId", requestId)
        .body(payment);
  }

}

❶ We use a logger to prove the right controller’s method gets the correct data when the endpoint is called.

❷ The app exposes the endpoint with HTTP POST at the path /payment.

❸ The endpoint needs to get a request header and the request body from the caller. The controller method gets these two details as parameters.

❹ The method sets a random value for the payment’s ID.

❺The controller action returns the HTTP response. The response has a header and the response body that contains the payment with the random ID value set.

You can now run this app, and it will start Tomcat on port 8080, which is the Spring Boot default, as we discussed in chapter 6. The endpoint is accessible, and you could call it with Postman. But the purpose of this chapter is to learn how to implement an app that calls the endpoint, so this is exactly what we will do next.