Skip to main content

Simple Spring Boot REST API

đź•“ 45 minutes

What you’ll learn​

  • How to set up a SpringBoot application.
  • Create a simple REST API using SpringBoot scaffolder.

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.


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

    • 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;
  • 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);
  • Add 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;
  • 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.

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

    package org.example.service.controller;

    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);
    • Please note that we "auto-wired" the service bean created in earlier steps.
  • 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 file in the project root or see the tutorial Java Spring Boot Local Development, section Prepare local development IDE.
    • If you need to change configuration parameters, for example the TCP port, please do so in codenow/config/application.yaml.
    • Build and run our application. By default it runs on http://localhost:8080/customers
    • You can use any browser navigating to our service URL. You will receive a JSON response.

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: