How to create a rest API using spring boot

In this post, we will study how to create a rest API using Spring Boot. We will be using Maven and Spring STS IDE to create a spring boot Project. You can also use eclipse to create a spring boot project.

Creating a Spring Boot Project

To create a new project, click on File -> New -> Spring Starter Project. Fill out information Group, Artifact, Description, etc. Now click on Next and Finish.

new spring starter project

After clicking on finish, the project structure will be like this:

project structure
In pom.xml, spring-boot-starter-parent will be automatically used as parent and spring-boot-starter as dependency after clicking on the finish. Apart from this, we need to add one more dependency spring-boot-starter-web for web dependencies ( Including embedded tomcat ).


Complete pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""
		<relativePath /> <!-- lookup parent from repository -->
	<description>Spring boot Rest Project</description>




Create first REST API

We will create REST API using @RestController annotation.

Create Model Class

First we will create a package com.codekru.model and create our model class Person.Java

package com.codekru.model;

public class Person {

    private String firstName;
    private String lastName;
    private int age;

    public Person() {

    public Person(String firstName, String lastName, int age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;

    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;

    public int getAge() {
        return age;

    public void setAge(int age) {
        this.age = age;

First we will create a package com.codekru.model and create our model class Person.Java

Create Controller Class

Now we will create a package com.codekru.controller and will create our rest controller there.

package com.codekru.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.codekru.model.Person;

public class PersonController {

    public List<Person> getPersonList() {
        List<Person> persons = new ArrayList<>();
        persons.add(new Person("Mohan", "tyagi", 20));
        persons.add(new Person("Raman", "Chhabra", 28));
        return persons;

@RestController -> This annotation will expose this URL as rest API. @RestController is a combination of @Controller and @ResponseBody annotation.

@RequestMapping -> This is the annotation in which specified URI mapping at which this rest API will be exposed.

We will run this application using

package com.codekru;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class Application {

    public static void main(String[] args) {, args);


We have not used @ComponentScan annotation at the Application class intentionally. This is not needed as the @RestController class is in one of the sub-packages of the Application class.

After running the application we can test it at URL: http://localhost:8080/
I have access to this URL using postman and the sample output is given below:

postman api hit

In this tutorial, we have learned how to create a rest API using spring boot. Hope you find this article useful… 🙂 Feel free to contact me with any questions!

Liked the article? Share this on

Leave a Comment

Your email address will not be published.