Skip to main content

Java Spring Boot REST API

đź•“ 45 minutes

What you’ll learn​

How to set up your application for:

  • getting data from REST API,
  • providing data to REST API.

In this tutorial, we will create a simple Java component with the Java Spring Boot scaffolder. We want to expose the single REST endpoint for getting user details for his username only. This will require simple sequential orchestration of two REST services, one to get user roles and the second for basic user details.

diagram

Project source​

This example project can be cloned from: http://gitlab.cloud.codenow.com/public-docs/java-spring-boot-demo/java-spring-boot-rest-api.git

Prerequisites​

Steps​

Open your IDE, import the created component and start coding:

  • Define the message payload. Here is an example of ClientInfo, which is a simple POJO with basic user details and roles:

    • Generate getters and setters with your IDE.

    • Examples of the ClientData and ClientAuthorization classes can be found in the example project repository.

      package org.example.service.model;

      import java.time.LocalDate;
      import java.util.Set;

      public class ClientInfo {

      private String username;
      private String firstname;
      private String surname;
      private LocalDate birthdate;
      private Set<String> roles;
      }
  • Next, create the classes ClientDataService and ClientAuthorizationService with the @Service annotation. These classes will represent http clients for calling the orchestrated endpoints. Example for ClientDataService:

  • In the baseUrl(), you should put the url to your existing component.

  • Create a new controller and put all parts together:

    • For more details about Spring Boot controllers, see: https://docs.spring.io/spring/docs/3.0.0.M4/reference/html/ch15s03.html

    • Create a new class ClientInfoController:

      package org.example.service.controller;

      import org.example.service.model.ClientData;
      import org.example.service.model.ClientInfo;
      import org.example.service.model.ClientAuthorization;
      import org.example.service.service.ClientDataService;
      import org.example.service.service.ClientAuthorizationService;
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      import reactor.core.publisher.Flux;

      @RestController
      @RequestMapping("data")
      public class ClientInfoController {

      private Logger log = LoggerFactory.getLogger(ClientInfoController.class);

      private ClientDataService clientDataService;
      private ClientAuthorizationService clientAuthorizationService;

      public ClientInfoController(ClientDataService clientDataService, ClientAuthorizationService clientAuthorizationService) {
      this.clientDataService = clientDataService;
      this.clientAuthorizationService = clientAuthorizationService;
      }

      @GetMapping("/clients/{username}")
      public Flux<ClientInfo> getClientInfo(@PathVariable String username) {
      log.info("GET client by username: {}", username);

      Flux<ClientData> clientDataFlux = clientDataService.getClientData(username);
      Flux<ClientAuthorization> clientRolesFlux = clientAuthorizationService.getClientAuthorization(username);

      return clientDataFlux.zipWith(clientRolesFlux, (data, roles) -> {
      ClientInfo response = new ClientInfo();

      response.setUsername(data.getUsername());
      response.setFirstname(data.getFirstname());
      response.setSurname(data.getSurname());
      response.setBirthdate(data.getBirthdate());
      response.setRoles(roles.getRoles());

      return response;
      });
      }
      }
  • Try to build and run the application in your IDE. After startup, you should be able to access your new controller's swagger: http://localhost:8080/swagger/index.html

    • For the correct setup, check the README.md file in the project root or see the tutorial Java Spring Boot Local Development, section Prepare local development IDE.
    • The component exposes a simple GET endpoint and can be tested using a browser call only. You should not forget to document your API and use the swagger-ui accordingly.
swagger

Deploy to CodeNOW​

If your code works in the local development, you are ready to push your changes to GIT and try to build and deploy your new component version to the CodeNOW environment.

What’s next?​

See our other developer guides: