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.
After clicking on finish, the project structure will be like this:
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 ).
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Complete pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.codekru</groupId>
<artifactId>spring-boot-rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-rest</name>
<description>Spring boot Rest Project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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() {
super();
}
public Person(String firstName, String lastName, int age) {
super();
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;
@RestController
public class PersonController {
@RequestMapping("/")
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 Application.java
package com.codekru;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, 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:
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!