Simple Spring Boot REST API

🕓 45 minutes

What you’ll learn:

  • How to setup a SpringBoot application.
  • In this tutorial we will create a simple REST API using SpringBoot scaffolder.
  • Later on we will add the security layer to it with Spring Security and Keycloak managed service.

Project Source

This sample project can be cloned from: http://gitlab.factory.codenow-control.codenow.com/public-docs/spring-boot-rest-api-secured-by-keycloak-example.git

Prerequisites

  • You are already familiar with CodeNow basics.
  • You are familiar with the concepts of REST web services.
  • You have already created a new application component with SpringBoot scaffolder. See the How To Create a New Application Component tutorial.
  • You have already set up your local development environment for Java. See the Java Spring Boot Local Development tutorial.
  • Cloned application GIT repository and opened project in IDE.
info

For the purposes of this tutorial we will not be using the pre-generated "Hello World" service and its classes "HelloWorldResponse" and "HelloWorldController". You can safely delete or ignore them.

Model classes

  1. Add the new model classes Customer and Order. They are very simple POJO classes with model attributes.

  2. Generate getters, setters and constructor. For example:

    01 package org.example.service.model;
    02
    03 public class Customer {
    04 private int id;
    05 private String firstName;
    06 private String lastName;
    07
    08 public Customer(int id, String firstName, String lastName) {
    09 this.id = id;
    10 this.firstName = firstName;
    11 this.lastName = lastName;
    12 }
    13
    14 public int getId() {
    15 return id;
    16 }
    16
    18 public void setId(int id) {
    19 this.id = id;
    20 }
    21 }
  3. Generate getters, setters and constructor.

Simple business service

  1. Add a new service interface, for example:

    01 package org.example.service.business;
    02
    03 import org.example.service.model.Customer;
    04 import java.util.List;
    05
    06 public interface CustomerService {
    07
    08 public List<Customer> getAll();
    09
    10 public Customer getById();
    11 }
  2. And its implementation emulating a simple business service:

    01 package org.example.service.business.impl;
    02
    03 import org.example.service.business.CustomerService;
    04 import org.example.service.model.Customer;
    05 import org.springframework.stereotype.Service;
    06
    07 import javax.annotation.PostConstruct;
    08 import java.util.ArrayList;
    09 import java.util.List;
    10
    11 @Service
    12 public class CustomerServiceImpl implements CustomerService {
    13 // this list serves only for testing purposes, real-world applications
    14 // will use appropriate resource managers instead
    15 private List<Customer> customers;
    16
    17 // initialize test data
    18 @PostConstruct
    19 private void initializeList() {
    20 customers = new ArrayList<>();
    21 customers.add(new Customer(1, "John", "Petrucci"));
    22 customers.add(new Customer(2, "Zack", "Wylde"));
    23 }
    24
    25 @Override
    26 public List<Customer> getAll() {
    27 return customers;
    28 }
    29
    30 @Override
    31 public Customer getById(int id) {
    32 for (Customer c : customers) {
    33 if (c.getId() == id)
    34 return c;
    35 }
    36
    37 return null;
    38 }
    39 }
  3. Please note that we annotated service with Spring's @Service so it can be recognized as a service bean and will be instantiated by dependency injection.

REST controller

  1. Add a new class representing a REST controller. For example:

    01 @RestController
    02 @RequestMapping("/customers")
    03 public class CustomerController {
    04 @Autowired
    05 CustomerService customerService;
    06
    07 @GetMapping
    08 public List<Customer> getAll() {
    09 return customerService.getAll();
    10 }
    11
    12 @GetMapping("/{id}")
    13 public ResponseEntity<Customer> getCustomer(@PathVariable int id) {
    14 Customer customer = customerService.getById(id);
    15
    16 if (customer != null)
    17 return new ResponseEntity<>(customer, HttpStatus.ACCEPTED);
    18 else
    19 return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    20 }
    21 }
  2. Please note that we "auto-wired" the service bean created in earlier steps.

  3. That's enough of coding and now to test our simple API.

Testing in local environment

  1. If you need to change configuration parameters, for example the TCP port, please do so in config/application.yaml.

  2. Build and run our application. By default it runs on http://localhost:8080/customers

  3. You can use any browser navigating to our service URL. You will receive a JSON response.

Testing in CodeNow environment

  1. After testing locally, commit changes to your repository and push it to CodeNow.

    git add .
    git commit -m "Initial commit"
    git push
  2. Follow the standard deployment process in CodeNow, see the Deploy application tutorial for details.

  3. Now you can click the "Open in browser" link under your application details. The swagger UI will open and you can test your service here.

openInBrowser

What’s next?

See our other developer tutorials: