Published on

Part 1: Let’s build a simple spring boot REST API - Initialize a spring boot project

Authors

To learn a new language or framework, I believe that making a simple todo REST API is the best way to start with. This is why we will build a simple REST API with Spring boot this time. This project will be multiple series and in part 1, we are going to initialize a spring boot project with vscode.

Source code for this article

https://github.com/ryuichi24/simple-spring-rest-api/tree/2-initialize-spring-boot-project

Set up a development environment

We are going to use vscode and docker for the development. To get started, we have to create a dev container for the Java development environment.

If you have not yet tried a dev container to set up a development environment, you can read one of my articles:

How to setup java development environment with docker dev container

Initialize spring boot project

If you follow along with How to setup java development environment with docker dev container, your project structure could be like this:

working-dir
├── .devcontainer
│   ├── .env
│   ├── devcontainer.json
│   ├── docker
│   │   └── workspace
│   │       └── Dockerfile
│   └── docker-compose.yml
├── .gitignore
├── README.md
└── src
    └── HelloWorld.java

From there, I will delete the src folder and initialize the spring boot project by using the Java extension.

Screen Shot 2022-08-25 at 13.48.55.png

Just open the vscode command pallet with Shift + Command + p or Shift + Ctl + p for windows, and type java: Create Java Project and select it.

Screen Shot 2022-08-25 at 13.49.48.png

And Select Spring Boot.

Screen Shot 2022-08-25 at 13.50.22.png

For the build tool, I prefer using Gradle, but you can choose whichever you want.

Screen Shot 2022-08-25 at 13.51.52.png

Select 2.7.3

Screen Shot 2022-08-25 at 13.52.04.png

The project language must be Java.

Screen Shot 2022-08-25 at 13.52.21.png

Here you can type in your own domain if you have or you can just type in whatever name you want. It is just for the uniqueness of the project.

Screen Shot 2022-08-25 at 13.53.00.png

Artifact Id is just the name of the project. This time I just go with simple-spring-rest-api.

Screen Shot 2022-08-25 at 13.53.12.png

Let’s do it with Jar.

Screen Shot 2022-08-25 at 13.53.21.png

The Java version must be 18.

Screen Shot 2022-08-25 at 13.54.19.png

As for the dependencies, I just chose three, which are Spring Web, Spring Boot DevTools, and Lombok. we can add more manually later.

Screen Shot 2022-08-25 at 13.54.31.png

And just return it or click OK.

If everything went perfectly, a new folder called simple-spring-rest-api should be created.

working-dir
├── .devcontainer
│   ├── .env
│   ├── devcontainer.json
│   ├── docker
│   │   └── workspace
│   │       └── Dockerfile
│   └── docker-compose.yml
├── .gitignore
├── README.md
└── simple-spring-rest-api # <- new !

Register the newly created project to Java Projects

Screen Shot 2022-08-25 at 14.11.10.png

Right after the spring boot project gets created, the Java Extension often does not detect the creation, so we have to let him know it!

Screen Shot 2022-08-25 at 14.12.34.png

Just open the vscode command pallet again, and type in relo and Developer: Reload Window should appear, and just select it.

Screen Shot 2022-08-25 at 14.12.51.png

On finishing the reload, on the right bottom, a popup should appear and it says Opening Java Projects: check details.

Screen Shot 2022-08-25 at 14.13.02.png

If you click the check details part, you can see the project importing logs in the integrated terminal.

Screen Shot 2022-08-25 at 14.24.36.png

Once the importing process ends, you can see that the simple-spring-rest-api project has been registered to the Java Projects.

Run the project

Finally, we can run the project, just open simple-spring-rest-api/src/main/java/tech/ryuichi24/simplespringrestapi/SimpleSpringRestApiApplication.java

# ommited
├── main
│   ├── java
│   │   └── tech
│   │       └── ryuichi24
│   │           └── simplespringrestapi
│   │               └── SimpleSpringRestApiApplication.java # <- this one!
│   └── resources
│       ├── application.properties
│       ├── static
│       └── templates
└── test
    └── java
        └── tech
            └── ryuichi24
                └── simplespringrestapi
                    └── SimpleSpringRestApiApplicationTests.java
Screen Shot 2022-08-25 at 14.43.08.png

At the top right corner, there is a triangle button, and you can click it to run the project.

Screen Shot 2022-08-25 at 14.47.10.png

If everything is perfect, you can see the spring boot logs in the terminal. If you have any problems, you can reload the window by typing in Developer: Reload Window in the vscode command pallet.

curl -v http://localhost:8080

The default port is 8080, and you can make an HTTP request to the port to check if the API is really running.

*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 
< Vary: Origin
< Vary: Access-Control-Request-Method
< Vary: Access-Control-Request-Headers
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Thu, 25 Aug 2022 05:49:10 GMT
< 
* Connection #0 to host localhost left intact
{"timestamp":"2022-08-25T05:49:09.846+00:00","status":404,"error":"Not Found","message":"No message available","path

If the API is running, the API will return a 404 response because there is no endpoint configured yet but at least, we were able to confirm the API is running.

Screen Shot 2022-08-25 at 15.01.55.png

Plus, since docker automatically forwards the port to the host, you can make an HTTP request from the browser in the host machine and get the HTTP response from the Spring Boot REST API.

Another way of initializing the Spring Boot Project

There is a website named “spring initializr”. You can do exactly the same thing as we did in the vsocde to initialize the Spring Boot Project.

Here is the link to the website: https://start.spring.io/

Conclusion

This time, we initialized a Spring Boot project from vscode, and ran the REST API.