DEV Community

Cover image for Beginner Spring Boot Series - Part 1
John Enad
John Enad

Posted on

Beginner Spring Boot Series - Part 1

Just as I was wrapping up my second FastAPI course, a thought occurred to me: How would this code look if it were written in Java/Spring Boot? Despite the differences between these two platforms, there are some similarities. So I went ahead and created a Spring Boot equivalent of my "Day 45: Beginner FastAPI Series - Part 2".

Before getting started, Here's the desired route definitions I referred to in the FastAPI Series:

GET /api/persons: Retrieves all persons
GET /api/persons/{id}: Fetches a specific person's details
POST /api/persons/{id}: Creates a new person record
PUT /api/persons/{id}: Updates an existing person's details
DELETE /api/persons/{id}: Removes a single person record

I use IntelliJ IDEA as my Java IDE, (My favorite Java IDE!) and to kick things off, I created a new project, and named it "enad-spring-1".

I managed to capture a couple of screenshots of my configuration selections. I also decided to just use Spring 2.7.12 and Java 17

Image description

Image description

Very much like what I mentioned in my initial post on FastAPI, what I'm presenting here is a preliminary setup. It's not a finished product, but a starting point designed to speed up our project's momentum. It's like a bunch of placeholder methods that currently return basic strings. However, in subsequent posts, we'll refactor them into full-fledged functions that interacts with the 'Person' entity.

So switching gears, here's the code in the 'PersonController' class within the controller package:

package com.johnenad.enadspring1.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/persons")
public class PersonController {

    @GetMapping("")
    public String getPersons() {
        return "read persons list";
    }

    @PostMapping("")
    public String createPerson() {
        return "create person item";
    }

    @GetMapping("/{id}")
    public String getPerson(@PathVariable("id") int id) {
        return String.format("read persons item id: %s", id);
    }

    @PutMapping("/{id}")
    public String updatePerson(@PathVariable("id") int id) {
        return String.format("update persons item id: %s", id);
    }

    @DeleteMapping("/{id}")
    public String deletePerson(@PathVariable("id") int id) {
        return String.format("delete persons item id: %s", id);
    }
}
Enter fullscreen mode Exit fullscreen mode

Edit: I've incorporated the 'open-api' dependency in the project's 'pom.xml' file to facilitate OpenAPI integration with Spring Boot.

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.7.0</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Spring Initializer generates this boilerplate code for our application as the starting point.

@SpringBootApplication
public class EnadSpring1Application {
    public static void main(String[] args) {
        SpringApplication.run(EnadSpring1Application.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

Launching the application is simple. Just open the file mentioned above, select Run - Run 'EnadSpring1Application', or use the keyboard shortcut, Shift-F10.

Once the application is up and running, fire up your browser and navigate to http://localhost:8080/api/persons. Voila! The application will respond with the text "read persons list". It's important to note that Spring Boot designates port 8080 as the default localhost port.

Test out the dynamic routes by visiting http://localhost:8080/api/persons/1. The terminal digit '1' in the URL is interpreted as the person's ID. Spring Boot's intuitive routing feature aligns the URL with the pre-established route, replacing {id} with '1'.

Now, let's revisit the 'Edit' segment I mentioned earlier. The 'springdoc-openapi-ui' dependency was added to furnish the application with interactive OpenAPI documentation.

To see this feature firsthand, enter: http://localhost:8080/swagger-ui.html

Image description

The interactive documentation allows you to test all the routes in your application. For instance, expand 'GET /api/persons', select the 'Try it out' option followed by the 'Execute' button to invoke the getPersons method, just as you would via your browser.

This marks the beginning of my Spring Boot series and it will be somewhat parallel at times with the FastAPI Python series. It's going to be exciting because we will be able to see both slowly evolve over time.

The next installment of this new Spring Boot series will go deeper into developing our 'Person' entity and making our methods work with it.

Note: In the next post, I will put the code in a public repo at Github

Top comments (0)