×
DocsConsole

DocsConsole

Spring Boot HelloWorld App with Maven

By | | Updated : 2018-10-07 | Viewed : 208 times

Spring Boot HelloWorld-App With Maven

The current article will explain above the maven installation. It will also give a walk through about basic application by using the maven. Here we will create a very basic application named Hello-World.

Maven installation

Here we will discuss the about the maven installation briefly. First of all, we have to make sure that JAVA_HOME variable should point to the JDK bin installation. Please find the below given screenshot for the same.

JAVA_HOME Set Up

Download preferable maven distribution zip from official website and then extract it in some windows file location. For all preferable maven archives, click here.

Download preferable maven distribution zip from official website and then extract it in some windows file location. For all preferable maven archives, click here.

Now we have to append the maven home in system variable PATH as given below.

Maven HOME Set Up

For more information about the installation please click here. You can check java and maven installations by using this commands.

For more information about the installation please click here. You can check java and maven installations by using this commands.

Verification for Java and maven installation

Spring Boot HelloWorld-App with Maven

First create the maven project in eclipse named SpringBoot-HelloWorld-App-Example. Please override existed maven pom.xml file with below given pom.xml.

<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>com.docsconsole.tutorials..springboot2</groupId>
	<artifactId>SpringBoot-HelloWorld-App-Example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBoot-HelloWorld-App-Example</name>
	<description>SpringBoot-HelloWorld-App-Example</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
	</parent>
	<!-- Additional lines to be added here... -->
	<!-- (you don't need this if you are using a .RELEASE version) -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
	
	<repositories>
		<repository>
			<id>spring-snapshots</id>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-milestones</id>
			<url>https://repo.spring.io/milestone</url>
		</repository>
	</repositories>
	
	<pluginRepositories>
		<pluginRepository>
			<id>spring-snapshots</id>
			<url>https://repo.spring.io/snapshot</url>
		</pluginRepository>
		<pluginRepository>
			<id>spring-milestones</id>
			<url>https://repo.spring.io/milestone</url>
		</pluginRepository>
	</pluginRepositories>

</project>

Here we used starters which will have all the required dependencies configuration. So we no need to configure the all the required dependencies. We can configure without parent starter as given below. Here we need to add dependency in dependencymanagement and need to remove parent tag as given below.

<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>com.docsconsole.tutorials..springboot2</groupId>
	<artifactId>SpringBoot-HelloWorld-App-Example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBoot-HelloWorld-App-Example</name>
	<description>SpringBoot-HelloWorld-App-Example-New</description>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<!-- Import dependency management from Spring Boot -->
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>2.1.4.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<repositories>
		<repository>
			<id>spring-snapshots</id>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-milestones</id>
			<url>https://repo.spring.io/milestone</url>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-snapshots</id>
			<url>https://repo.spring.io/snapshot</url>
		</pluginRepository>
		<pluginRepository>
			<id>spring-milestones</id>
			<url>https://repo.spring.io/milestone</url>
		</pluginRepository>
	</pluginRepositories>
</project>

Now we need to create one controller file, which will response for each http request. Here we are simply displaying the message on the browser. Please find the below given code controller file.

HelloWorldController.java
package com.docsconsole.tutorials.springboot2.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
	@RequestMapping("/")
	 String home() {
	 return "Hello Spring Boot World!";
	 }
}

Spring boot application works like standalone application. Simply we need to write the main client which can be useful for starting the embedded server in spring boot. Find the below given code for main client.

MainClient.java
package com.docsconsole.tutorials.springboot2;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;

@SpringBootApplication
public class MainClient {

	public static void main(String[] args) throws Exception {
		SpringApplication.run(MainClient.class, args);
	}
}

    Code explanation:

  • @SpringBootApplication is equivalent annotation of @Configuration, @EnableAutoConfiguration, and @ComponentScan.

  • @Configuration: Spring configuration will be done in two ways XML or Java. @configuration will be used for Java configuration.

  • @EnableAutoConfiguration it enables the Spring Boot for automated configuration. Here based on the dependencies configured in pom.xml Spring Boot will behave accordingly.

  • @ComponentScan will be used for identifying the configured spring classes in particular package.

After completion of all the required files, project structure will be as given below.

Maven Spring Boot HelloWorld App

Run (Run as Java application) the MainClient.java then embedded server in Spring Boot will be started as shown below.

Spring Boot HelloWorld Example

After firing the http://localhost:8080/ URL, you can see the below given out put on the browser.

The result of Spring Boot HelloWorld App Example
Leave A Reply