Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
Creating a RESTful API with Spring Boot is quite straightforward due to the framework's extensive support for building web applications. Here's a step-by-step guide to building a basic REST API using Spring Boot:
You can bootstrap a new Spring Boot project using Spring Initializr. Select the necessary dependencies, especially "Spring Web" to enable web support.
After creating the project, you will have the following structure:
src/main/java
: Java source files.src/main/resources
: Configuration and static files.src/test/java
: Test source files.This represents a data object you want to manage with your REST API. For instance, let's manage a simple Book
entity.
public class Book { private Long id; private String title; private String author; // Constructors, getters, setters, etc. }
Controllers manage incoming HTTP requests and send responses. Here's a basic BookController
:
@RestController @RequestMapping("/api/books") public class BookController { // For the sake of simplicity, let's use an in-memory list private List<Book> books = new ArrayList<>(); @GetMapping public List<Book> getAllBooks() { return books; } @GetMapping("/{id}") public Book getBook(@PathVariable Long id) { return books.stream() .filter(b -> b.getId().equals(id)) .findFirst() .orElse(null); // Add proper error handling here } @PostMapping public Book createBook(@RequestBody Book book) { books.add(book); return book; } // You can also add PUT and DELETE endpoints as needed }
Most of Spring Boot's configuration is convention-over-configuration, so it works out of the box. However, you can specify properties in application.properties
or application.yml
in src/main/resources
.
For instance, to change the port:
server.port=8085
Run the main class generated by Spring Initializr (it should have @SpringBootApplication
annotation) either from your IDE or by using Maven/Gradle.
For Maven:
mvn spring-boot:run
For Gradle:
./gradlew bootRun
With your application running, you can test the API using tools like Postman, Insomnia, or just using curl
from the command line.
For example:
curl -X GET http://localhost:8085/api/books
This is a simple and straightforward way to create a RESTful API using Spring Boot. As you develop more complex applications, you may need to add more configurations, services, repositories, and other components.
Building a Hello World RESTful API with Spring Boot:
@RestController public class HelloWorldController { @GetMapping("/hello") public String sayHello() { return "Hello, World!"; } }
Creating RESTful endpoints in Java with Spring Boot:
@RestController public class ApiController { @GetMapping("/api/greet") public String greet() { return "Greetings!"; } // Add more endpoints for POST, PUT, DELETE, etc. }
Basic controller and request mapping in Spring Boot for REST:
@RestController @RequestMapping("/api") public class ApiController { @GetMapping("/resource") public String getResource() { return "This is a REST resource!"; } }
Using annotations for RESTful services in Spring Boot:
@RestController
and @RequestMapping
to simplify the creation of RESTful services.@RestController @RequestMapping("/api") public class ApiController { @GetMapping("/data") public String getData() { return "Data from RESTful service"; } }
Handling request parameters and path variables in Spring Boot:
@GetMapping("/user") public String getUser(@RequestParam String username) { return "Hello, " + username + "!"; } @GetMapping("/user/{id}") public String getUserById(@PathVariable Long id) { return "User ID: " + id; }
Returning JSON responses from a Spring Boot REST API:
@RestController public class UserController { @GetMapping("/user") public User getUser() { User user = new User("John", "Doe"); return user; } } // User class with getters and setters