Create a REST API with Spring Boot

How to create a REST API with Spring Boot? To begin with, let's see what is Spring Boot.

Spring Boot is a framework that bootstraps a Java web application without the XML configuration headaches that come with a typical Spring application. The framework adopts an opinionated approach to configuration by making decisions based on the use cases that fit the majority of modern web apps. In other words, it follows the philosophy of convention over configuration.

With Spring Boot, we can create an application that bundles all dependencies and its servlet container so it doesn't even require a traditional WAR deployment. We'll have a fat executable JAR that can be run as a standard Linux service.

This article will give an idea of how to quickly get started with springboot. And I will explain how you can create a REST service using Spring Boot.

How to Create Project and Setup

1. Create a new Project.

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

2. Add the following SpringBoot dependencies to the POM file and save.

3. Create Person class as follows.

package com.learnjava.model;
public class Person {
  
  private int id;
  private String name;
  private int age;
  
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  
  public Person(){
    
  }
  
  public Person(int id,String name, int age) {
    super();
    this.id=id;
    this.name = name;
    this.age = age;
  }
//getter and setter methods
}

4. Create a PersonController class as follows:

package com.learnjava.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.learnjava.model.Person;
@RestController
public class PersonController {
  
  @GetMapping("/person")
  public List<Person> getAllPersons(){
    //Returns hardcoded data, a real world application would return from the database
    List<Person> personList = new ArrayList<Person>();
    personList.add(new Person(1,"Mickey Mouse", 30));
    personList.add(new Person(2,"Donald Duck", 35));
    personList.add(new Person(3,"Peppa Pig", 15));
    return personList;
  } 
  
  @GetMapping("/person/{personId}")
  public Person getPersonWithId(@PathVariable Integer personId){
    //Returns hardcoded data, a real world application would return from the database
    return new Person(3,"Peppa Pig", 15);
  } 
  
  
  @PostMapping("/person/newperson")
  public void addPerson(@RequestBody Person person){
    //Just has a Sysout stmt, a real world application would save this record to the database
    System.out.println("Saving person information");
    
  }
}

5. Finally, create a Main.java as follows:

package com.learnjava;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class Main {
    public static void main(String[] args) {
          SpringApplication.run(Main.class, args);
      }
}

6. Run Main.java as a Java application.

How to Test It Via Browser

1. Get All Persons.

Open a browser window and type the following URL:

http://localhost:01/person/

2. Get Person By Id.

Open a browser window and type the following URL:

http://localhost:01/person/3

3. Add Person.

This cannot be tested in a Browser as it is a POST request.



Leave a reply



Submit