Build a Restful API with Java, Maven and Spring Boot using Vim on OSX

  • Curl
  • Unzip
  • Maven
  • OpenJDK
  • Vim / NeoVim
brew install curl unzip openjdk@11

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

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
unzip demo.zip
(super exciting, right?)

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>

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);
}
}
import java.util.list;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
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
@RestController
public 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
mvn exec:java -Dexec.mainClass=com.example.demo.DemoApplication

--

--

--

Programmer / Artist / Believer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Python And Flask Framework Of Python Course For Beginners | FREE

Reducing Enumerable — Part Six: A Final Lesson from Scarlet

The Python Programming For Anyone Immersive Training | Free Course

Nio, Time to programming in smart phone

Leetcode — 47. Permutations II

Localization Changes in Java 9

Set Up Windows 11 For Valorant — Fix TPM 2.0 & Secure Boot Issues

How to make Python Packages part of Yocto generated Image?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lucas Thinnes

Lucas Thinnes

Programmer / Artist / Believer

More from Medium

Azure Cosmos DB— RBAC Authentication using a Java Client

Spring boot docker application deployment using Jenkins pipeline

Nginx File Configuration and Logs (Part 3)

Geospatial indexing app with different backends using Spring Boot and Testcontainers