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:


  • 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.

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:

    package org.example.service.model;
    public class Customer {
    private int id;
    private String firstName;
    private String lastName;
    public Customer(int id, String firstName, String lastName) { = id;
    this.firstName = firstName;
    this.lastName = lastName;
    public int getId() {
    return id;
    public void setId(int id) { = id;
  3. Generate getters, setters and constructor.

Simple business service#

  1. Add a new service interface, for example:

    import org.example.service.model.Customer;
    import java.util.List;
    public interface CustomerService {
    public List<Customer> getAll();
    public Customer getById(int id);
  2. And its implementation emulating a simple business service:

    import org.example.service.model.Customer;
    import org.springframework.stereotype.Service;
    import javax.annotation.PostConstruct;
    import java.util.ArrayList;
    import java.util.List;
    public class CustomerServiceImpl implements CustomerService {
    // this list serves only for testing purposes, real-world applications
    // will use appropriate resource managers instead
    private List<Customer> customers;
    // initialize test data
    private void initializeList() {
    customers = new ArrayList<>();
    customers.add(new Customer(1, "John", "Petrucci"));
    customers.add(new Customer(2, "Zack", "Wylde"));
    public List<Customer> getAll() {
    return customers;
    public Customer getById(int id) {
    for (Customer c : customers) {
    if (c.getId() == id)
    return c;
    return null;
  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:

    public class CustomerController {
    CustomerService customerService;
    public List<Customer> getAll() {
    return customerService.getAll();
    public ResponseEntity<Customer> getCustomer(@PathVariable int id) {
    Customer customer = customerService.getById(id);
    if (customer != null)
    return new ResponseEntity<>(customer, HttpStatus.ACCEPTED);
    return new ResponseEntity<>(HttpStatus.NOT_FOUND);
  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.


What’s next?#

See our other developer tutorials: