- Published on
Part 1: Let’s build a simple spring boot REST API - Initialize a spring boot project
- Authors
- Name
- Ryuichi Nishi
- @ryuichi2c
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.
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.
And Select Spring Boot
.
For the build tool, I prefer using Gradle, but you can choose whichever you want.
Select 2.7.3
The project language must be Java
.
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.
Artifact Id is just the name of the project. This time I just go with simple-spring-rest-api
.
Let’s do it with Jar
.
The Java version must be 18.
As for the dependencies, I just chose three, which are Spring Web
, Spring Boot DevTools
, and Lombok
. we can add more manually later.
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
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!
Just open the vscode command pallet again, and type in relo
and Developer: Reload Window
should appear, and just select it.
On finishing the reload, on the right bottom, a popup should appear and it says Opening Java Projects: check details
.
If you click the check details
part, you can see the project importing logs in the integrated terminal.
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
At the top right corner, there is a triangle button, and you can click it to run the project.
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.
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.