Java: Get Started with Spring Boot

remko de knikker
NYC⚡️DEV
Published in
6 min readJan 22, 2018

We live in a world of buzz and jazz, if it’s not hot, if it’s not outrageous and new, it’s out, it’s old and gets very little attention. And the world of technology is not escaping this fire and fury of today’s social media. Every day, one feels that haunting anxiety of yet another rising star at the JavaScript firmament: Vue.js, Nerv, GraphQL. But those are not the technologies that form the backbone of the Internet economy? Very often, the real ‘Deus ex Machina’ of the modern technology is still Java.

To a large degree, programming trends now are determined by consumer facing front-ends, websites, mobile apps, smart gadgets, and smart devices. JavaScript is often literally the superficial trends of these technologies. It is easy sometimes to forget that these are running for a large part on corporate legacy infrastructure that is still largely dominated by Java.

Without a doubt, a trending frameworks in the Java world is Spring. In this article I want to get started with Spring Boot. In the following article I will create a Spring Client, Spring REST service, and connect the client to the service via the Spring Service Registry.

Requirements

This tutorial consists of the following steps:

  • Create a Java Application,
  • Add Maven,
  • Add Spring Boot
  • Add a Little Extra Spring

Create a Java Application

First, let’s create a plain old Java application as the starting point, and I will then convert it to a Maven project and finally into a Spring Boot project. The Spring Boot starter guide uses a starter project, but these starter applications always hide parts of the raw code, which makes them practically useless in my opinion.

I prefer guides that build everything from scratch with a text editor and command line. In the following instructions, I use Eclipse, but you can easily substitute Eclipse with Sublime or Atom or any other text editor.

  • Create a new Java Project in Eclipse called ‘SpringClient’, and click ‘Finish’,
  • Create the following source directories: ‘~/src/main/java’, ‘~/src/test/java’, ‘~/src/main/resources/META-INF’,
  • Create a build folder ‘~/target/classes’,
  • To configure the Eclipse project settings, right-click the project name ‘SpringClient’ in the ‘Project Explorer’ in Eclipse, click Properties, select ‘Java Build Path’ and open the ‘Source’ tab,
  • Select the ‘SpringClient/src’ folder and remove it, click Apply,
  • Click ‘Add Folder’, click ‘Create New Folder’, type ‘src/main/java’, click ‘Finish’, click ‘OK’,
  • Click ‘Add Folder’, click ‘Create New Folder’, type ‘src/test/java’, click ‘Finish’, click ‘OK’,
  • Click ‘Add Folder’, click ‘Create New Folder’, type ‘src/main/resources/META-INF’, click ‘Finish’, click ‘OK’,
  • Click ‘Apply and Close’,
  • In the source folder ‘~/src/main/java’, create a new Package ‘hello’ and a new Class file ‘Application.java’ in the ‘hello’ package,
package hello;public class Application {public static void main(String[] args) {
System.out.println("hello world");
}
}
  • In the ‘~/src/main/resources/META-INF’ folder, create a new file ‘MANIFEST.MF’,
Manifest-Version: 1.0
Class-Path: ./classes
Main-Class: hello.Application
  • Make sure the MANIFEST.MF file ends with a new line or carriage return or the manifest will not parse properly,
  • Now, compile, package and run the application,
$ javac -cp ./src/main/java src/main/java/**/*.java -d ./target/classes
$ jar cfm ./target/SpringClient-0.1.0.jar ./src/main/resources/META-INF/MANIFEST.MF -C ./target/classes .
$ java -jar ./target/SpringClient-0.1.0.jar

Add Maven

Well done, but let’s make life easier, and turn the project into a Maven project.

  • Add a new file ‘~/pom.xml’,
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>remkohdev</groupId>
<artifactId>SpringClient</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>
<name>SpringClient</name>
<url>https://github.com/remkohdev/spring</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.main.class>hello.Application</java.main.class>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-shade-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>hello.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
  • Now clean the build target first, package and run the application,
$ mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------------------------------------------------
[INFO] Building SpringClient 0.1.0
[INFO] --------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ SpringClient ---
[INFO] Deleting /Users/user1/src/SpringClient/target
[INFO] --------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------------------
[INFO] Total time: 0.358 s
[INFO] Finished at: 2019-10-04T10:45:37-04:00
[INFO] Final Memory: 9M/309M
[INFO] --------------------------------------------------------------------
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------------------------------------------------
[INFO] Building SpringClient 0.1.0
[INFO] --------------------------------------------------------------------
... and more
[INFO] --------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------------------
[INFO] Total time: 4.253 s
[INFO] Finished at: 2019-10-04T10:47:08-04:00
[INFO] Final Memory: 30M/257M
[INFO] --------------------------------------------------------------------
$ java -jar ./target/SpringClient-0.1.0.jar
hello world

The Maven Shade Plugin is bound to the ‘package’ phase and is used to package the artifact in an uber-jar, including its dependencies, and creates a shaded or renamed jar in the ‘target’ directory. Run the ‘SpringClient’ application with the ‘java -jar’ command.

Add Spring Boot

Now, that was clever, but let’s humble ourselves and add Spring Boot.

  • Start by adding Spring Boot to the Application class,
package hello;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
System.out.println("Hello world");
}
}
  • Edit the pom.xml Maven build file,
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>remkohdev</groupId>
<artifactId>SpringClient</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>
<name>SpringClient</name>
<url>https://github.com/remkohdev/springclient</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.main.class>hello.Application</java.main.class>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.9.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-shade-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.9.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
  • Optionally, you can generate the Maven Wrapper,
  • Do a clean Maven install and run the application with Maven Wrapper,
$ mvn -N io.takari:maven:wrapper
$ ./mvnw clean install
$ ./mvnw spring-boot:run
  • Run the same commands without the wrapper,
$ mvn clean install
$ mvn spring-boot:run
  • If you don’t use the Maven Wrapper, you might have to repackage the application sometimes, you can add the ‘repackage’ goal to the ‘executions’ property of the ‘spring-boot-maven-plugin’ in the ‘pom.xml’ file,
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
  • and run a clean install, repackage and run the application,
$ mvn clean install spring-boot:repackage
$ java -jar target/SpringClient-0.1.0.jar

Add a Little Extra Spring

Finally, I will implement CommandLineRunner and add a MessageService.

  • Create a new class ‘MessageService.java’ in the ‘hello’ package,
package hello;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class MessageService {

@Value("${name:unknown}")
private String name;

public String getMessage() {
return getMessage(name);
}

public String getMessage(String name) {
return "Hello " + name;
}
}
  • Create a new file ‘~/application.properties’ in the ‘src/main/resources’ directory,
name=mars
  • And edit the class ‘Application.java’,
package hello;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.CommandLineRunner;
import org.springframework.beans.factory.annotation.Autowired;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private MessageService helloService;
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(Application.class);
app.run(args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(helloService.getMessage());
}
}
  • Run a clean install, repackage and run the application,
$ mvn clean install spring-boot:repackage
$ mvn spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------------------------------------------------
[INFO] Building SpringClient 0.1.0
[INFO] --------------------------------------------------------------------
[INFO]
... and more
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.9.RELEASE)

2019-10-04 11:10:25.102 INFO 88139 --- [ main] hello.Application : Starting Application on User1-MBP with PID 88139 (/Users/user1/src/SpringClient/target/classes started by user1 in /Users/user1/src/SpringClient)
2019-10-04 11:10:25.104 INFO 88139 --- [ main] hello.Application : No active profile set, falling back to default profiles: default
2019-10-04 11:10:25.160 INFO 88139 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1cbac312: startup date [Fri Oct 04 11:10:25 EDT 2019]; root of context hierarchy
2019-10-04 11:10:25.528 INFO 88139 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-10-04 11:10:25.717 INFO 88139 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
Hello mars2019-10-04 11:10:25.734 INFO 88139 --- [ main] hello.Application : Started Application in 0.994 seconds (JVM running for 3.995)
[INFO] --------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------------------
[INFO] Total time: 2.947 s
[INFO] Finished at: 2019-10-04T11:10:25-04:00
[INFO] Final Memory: 24M/375M
[INFO] --------------------------------------------------------------------
2019-10-04 11:10:25.867 INFO 88139 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1cbac312: startup date [Fri Oct 04 11:10:25 EDT 2019]; root of context hierarchy
2019-10-04 11:10:25.868 INFO 88139 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown

You should see “Hello mars” in the output.

Peace.

--

--

remko de knikker
NYC⚡️DEV

Cloud Native Developer Advocate @IBMDeveloper for Cloud Native, Containers, Kubernetes, Security and DevOps. Dutch NYer, dad, humanist with empathy for paradox.