Site icon Narayana Tutorial

Spring Boot Web Example

Spring Boot Web Example

 

In this article, I am going to showing how to create a spring boot web example project.

Pre-Requisites

  1. Java 1.8
  2. Spring Boot 2.2.5.RELEASE
  3. Eclipse IDE

Spring boot project can be created in multiple ways.

  1. Eclipse Maven
  2. STS
  3. start.spring.io

Here I am going to show start.spring.io using to create spring boot project.

Step 1:

Open any browser in your system and type https://start.spring.io/ in the address bar

Spring-Boot-Web-1

 

 

Step 2:

Project:  Maven Project

Language: Java

Spring Boot:  2.2.5

Project Metadata :`

Group: com.narayanatutorial

Artifact: SpringBootWebExample

Click on Options, Select War in Packaging

Dependencies: nothing select because by default spring started included.

Spring-Boot-Web-2

Step 3:

Unzip the downloaded project in your system and then import it into eclipse as maven project.

Open Eclipse, go to File -> Import -> Maven ->  Existing Maven Projects –> click on Next — > browse the unzipped folder and then click on finish.

Spring-Boot-Maven-Import

 

Step 4:

The project was imported successfully and then execute the main java program.

Source Code Details

 

SpringBootWebExampleApplication.java

package com.narayanatutorial.SpringBootWebExample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWebExampleApplication {

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

}

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.5.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.narayanatutorial</groupId>
	<artifactId>SpringBootWebExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBootWebExample</name>
	<description>Demo project for Spring Boot</description>
	<packaging>war</packaging>
	<properties>
		<java.version>1.8</java.version>
	</properties>

	<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>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

Get full source code from git.