Create a Web Application with Spring 5



Overview

The tutorial illustrates how to Create a Web Application with Spring. We’ll look into the Spring Boot solution for creating the application.

We’ll primarily use Java configuration in this tutorial.

Pre-requisties

  • JDK 1.8
  • Maven
  • IDE (for ease of use)

Creating Web app using Spring Boot

First, we’ll add the required dependencies and plugins in pom.xml

We will use the dependencyManagement to define a standard version of an artifact to use across multiple projects.

We will import version of artifacts defined in spring-boot-starter-parent.

1
2
3
4
5
6
7
8
9
10
11
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Now we’ll add the spring-boot-starter-web dependency.

Spring Boot Starter Web includes:

  • spring-web module
  • spring-webmvc module
  • tomcat starter

Spring-web & Spring-webmvc are required for spring appplication while tomcat starter is required to run web application directly without explicitly installing any server.

1
2
3
4
5
6
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

Spring Boot Maven Plugin provides Spring Boot support in Maven, letting you package executable jar or war archives and run an application “in-place”.

1
2
3
4
5
6
7
8
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Creating a Spring Boot Application

Create a class with main method. Annotate the class with @SpringBootApplication. Call the run method of SpringApplication class

1
2
3
4
5
6
7
8
9
10
11
12
package com.tutorialflix.spring.web;

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

@SpringBootApplication
public class SpringBootWebApplication {

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

@SpringBootApplication includes the functionality of following annotations:

  • @Configuration
  • @EnableAutoConfiguration
  • @ComponentScan.

By default, it will perform the component scan in base package. So make sure SpringBootWebApplication class is present in the base package.

Creating a Web Controller

Create a class with a meaningful name based on your requirement & annotate the class with @Controller.

1
2
3
4
5
6
7
8
9
package com.tutorialflix.spring.web.contrroller;

import org.springframework.stereotype.Controller;

@Controller
public class MyWebController {


}

Create a method to accept requests

Create a method named home to accept the request on ‘/‘ url and show the index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.tutorialflix.spring.web.contrroller;

import org.springframework.stereotype.Controller;

@Controller
public class MyWebController {

@GetMapping("/")
public String home() {
return "index.html";
}

}

Create a webpage to display

Create a public folder inside resources folder of application and create a file named index.html.

1
2
3
4
5
<html>
<body>
<h1>Welcome to TutorialFlix.</h1>
</body>
</html>

Configure application using properties file

Create a file application.properties inside resources folder of application and following properties.

1
2
server.port=8081
logging.level.org.springframework.web=DEBUG

server.port : to change the default port 8080 to any other port number.

logging.level.org.springframework.web : to change the log level default INFO to DEBUG

Run the web application

1
$ mvn spring-boot:run

Test

Goto http://localhost:8081/

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×