Overview:

In this tutorial, we will be covering how to set up a simple REST API using Spring Boot. We will be setting up the API from scratch using Eclipse and the Spring STS Plugin. At the end of this tutorial, you will be able to:

  • Create a new Spring Boot REST API
  • Define classes that handle incoming requests
  • Generate “Fat Jar” file
  • Run / Test your new Spring Boot API

Requirements:

Before we get started:

In this tutorial, I am using the Eclipse Java EE 4.7.2 (Oxygen.2) and Spring Tools Suite 3.9.5-RELEASE. If you are using a different IDE or version of Eclipse, there may be minor discrepancies in screenshots. If you’d like to follow along with the tutorial without creating the project from scratch, here’s the GitHub repository: https://github.com/codetober/Spring-Boot-REST-API-demo

Create a New Project

  1. Press “File” > “New” > “Spring Starter Project
    1. Requires STS Plugin
  2. Complete the project details
  3. Press “Next
  4. Configure the Project
    1. Select “2.0.3” as the Spring Boot Version
    2. In the Available: input type “rest
    3. Select “Web” > “Rest Repositories” from the options
    4. Press “Next”
  5. Press “Finish”
    Note: The STS Plugin uses https://start.spring.io/ Spring Initializr to create the base project.

Creating a Data Model:

In this tutorial, the data we will return is a simple Java Object, just for fun, let’s create the Dog class.

  1. Create the Dog.class file
    package com.codetober.demoapi;
    
    public class Dog {
    	
    	private final String name;
    	private final String breed;
    	private final String color;
    	private final int tagNumber;
    	
    	public Dog(String name, String breed, String color, int tagNumber) {
    		this.name = name;
    		this.breed = breed;
    		this.color = color;
    		this.tagNumber = tagNumber;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public String getBreed() {
    		return breed;
    	}
    
    	public String getColor() {
    		return color;
    	}
    
    	public int getTagNumber() {
    		return tagNumber;
    	}
    }
    
    

Creating Rest Controller

  1. Create DogController.class file
  2. @RestController Annotation on the Controller Class
    1. @RequestMapping(“/dogs”) to set the controller path
  3. @RequestMapping(“tag/{tagNumber}”) on the method
    1. Setting method to GET only
    2. Spring boot uses Jackson 2 to Transform the Dog to application/json output
      package com.codetober.demoapi;
      
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import org.springframework.web.bind.annotation.RestController;
      
      @RestController()
      @RequestMapping("/dogs")
      public class DogController {
      	
      	@RequestMapping( method=RequestMethod.GET, value="/tag/{tagNumber}")
      	public Dog findDogByTagNumber(@PathVariable("tagNumber") int tagNumber) {
      		// Check if tagNumber is even
      		if(tagNumber % 2 == 0) {
      			// Even
      			return new Dog("Turbo", "Dingo", "Brown", tagNumber);
      		} else {
      			// Odd
      			return new Dog("Carat", "Terrier Mix", "White", tagNumber);
      		}
      	}
      }
      

 

Generate “Fat Jar” File

  1. From the Project Explorer: “Right-Click” > “Run As…” > “Maven Install”
    1. Make sure that the spring-boot-maven-plugin is in the POM file
  2. The Jar file will be in the <project_directory>/target, feel free to copy the Jar to any directory you want

 

Run / Test the API!

  1. Open Terminal or CMD and navigate to the location of the Jar file.
  2. type java -jar <jar name>
    1. If you followed this tutorial exactly, the <jar name> is “demo-api-0.0.1-SNAPSHOT.jar
  3. Open your browser and navigate to:
    1.  localhost:8080/dogs/tag/1
      {"name":"Carat","breed":"Terrier Mix","color":"White","tagNumber":1}
    2.  localhost:8080/dogs/tag/2
      {"name":"Turbo","breed":"Dingo","color":"Brown","tagNumber":2}
  4. In the open CMD or Terminal, simply press Control + C to terminate the API

 

That’s it! Congratulations on making it all the way through this tutorial and continuing your quest to craft amazing code! If you liked this tutorial, please share it 🙂 Comment below with any questions or issues you faced with this tutorial.