This is a complete tutorial of building a REST api with Spring Boot with. You will learn how to :
- Create REST APIs
- Add spring security with jwt authentication & authorization
- Work with database
- Add database migration with flyway
The source code of this tutorial is published in git : https://github.com/farhantanvirtushar/spring-rest-demo
Creating A Spring Boot REST API
Create a spring boot application from Spring Initializr with Java version 17. Then add following dependency in pom.xml :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
Create a new package named controllers. Then create a new controller named HomeController.java inside our newly created controllers package. Write the following code inside HomeController.java :
package com.example.springrestdemo.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/rest/home")
public class HomeController {
@ResponseBody
@RequestMapping(value = "",method = RequestMethod.GET)
public String hello(){
return "hello world";
}
}
We have created an api endpoint “/rest/home” for GET request. Now run the application through IDE or through command line with the following command