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
- Java,
- Maven,
- Eclipse,
- Spring Tool Suite (STS),
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 directories: ‘~/src/main/java’, ‘~/src/test/java’, ‘~/src/main/resources/META-INF’, and ‘~/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 ‘Apply and Close’,
- In the source folder ‘~/src/main/java’, create a new Package ‘hello’ and a new Class file ‘hello.Application.java’,
package hello;
public class Application {public static void main(String[] args) {
System.out.println("hello fool");
}
}- 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/classesjar 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
$ mvn package
$ java -jar ./target/SpringClient-0.1.0.jar
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.
- Let 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 fool");
}
}- 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/spring</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>
</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>
</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>
- Generate the Maven Wrapper, do a clean install and run the application with Maven Wrapper,
$ mvn -N io.takari:maven:wrapper
$ mvn clean install
$ ./mvnw spring-boot:run- If you don’t want to use the Maven Wrapper, 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 ‘hello.MessageService.java’,
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’,
name=fool
- And edit the class ‘hello.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
$ java -jar target/SpringClient-0.1.0.jar
Peace.
No comments:
Post a Comment