Build a Restful API with Java, Maven and Spring Boot using Vim on OSX
This week I am going to explain a method I found for launching a server and creating restful APIs which can be done entirely from the CLI!
To get started you will need the following dependencies installed on your machine:
- Java
- Curl
- Unzip
- Maven
- OpenJDK
- Vim / NeoVim
Assuming you already have at least Java and Vim on your machine, Curl, Unzip, and OpenJDK can all be installed from the terminal via this command:
brew install curl unzip openjdk@11
Maven is a bit more involved of an installation procedure, so I will start with that.
INSTALLING MAVEN
Maven will be what we use to launch the server and build our package. It can be installed for your machine from their site here. After downloading, unzip the directory and place it where the rest of your DevOps are located. I placed mine within my user directory for ease of use and location. Next, you will want to point to the path of the directory using these two commands, mine looked like this:
export M2_HOME=/Users/Luke/apache-maven-3.8.3/
export PATH=$PATH:/Users/Luke/apache-maven-3.8.3/bin
After setting these, you should be able to see confirmation via the terminal that they are installed and referenced correctly by typing mvn — v:
If this didn’t work correctly, try adding the second line of code above to your .bshrc or .zshrc depending on which you use or restarting your terminal window. Feel free to comment below if you get stuck on this part!
INITIALIZING A SPRING BOOT PROJECT
This can be done in one command. Navigate to an empty directory named to your desire, I called mine “springboot-api”. From the terminal, execute this command:
curl https://start.spring.io/starter.zip -d javaVersion=11 -o demo.zip
If this was executed correctly, you should end up with a .zip folder within the directory you created. This can alternately be done from their website but, who needs that when you have NeoVim? ;)
The directory can be unzipped from the command line using the unzip command:
unzip demo.zip
And voilá! We have the Spring Boot project just like that!
ADDING DEPENDENCIES
There are three dependencies we will need to get this server up and running, so navigate over to the pom.xml file. POM stands for “Project Object Model” and basically contains the things like versions and dependencies. We will want our dependency section to look like this:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
I believe there are ways of importing these by default when you initialize the Spring Boot project but I haven’t figured out the exact syntax, so if you happen to see this and know, feel free to mention it below!
ADDING AN ENDPOINT
Navigate into src/main/java/com/example/demo and open up the DemoApplication.java. The file should look like this:
package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}}
To add the ability to create an endpoint, we will need to import the ability to make a list of strings as well as the RestController and GetMapping frameworks. The two of these can be imported from Spring Boot and will be placed below the other import statements listed above:
import java.util.list;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
Now, beneath the import statements we can call the RestController via (added in code form to avoid “atting” anybody with this username on Medium):
@RestController
Next, we can add the endpoint below the main method. I made a super simple array which contains the two words “Dog” and “Attack”. The final code ended up looking like this:
package com.example.demo;import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@SpringBootApplication
@RestControllerpublic class DemoApplication {public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}@GetMapping
public List<String> dogAttack() {
return List.of("Dog", "Attack");
}}
BUILDING AND LAUNCHING THE SERVER
We are now getting to the compilation and launching part of this lesson, which will be the final one! Give that file a good ol’ :w and navigate in your terminal to the primary directory. Once you are there, enter this command:
mvn compile
This will compile everything that is inside the directory to a launchable project. After that, we will enter this wild command to start the server:
mvn exec:java -Dexec.mainClass=com.example.demo.DemoApplication
This will give us a server success or error depending on how well you followed the instructions ;)
You can see (or maybe not, my bad for the wide screenshot) that the server is now running on port 8080. Head on over to http://localhost:8080 to check out what we have just done:
Just like that! I hope the potential of this incredibly simple API gives you ideas on how to conquer your wildest dreams!