Home Spring SpringBoot How to set up and run Springboot webapp in 2 steps?

How to set up and run Springboot webapp in 2 steps?

0
Springboot webapp

In this article, we’ll explore of Developing a Spring Boot Web Application. We’ll explore how Spring Boot will help you speed up the development of your application. We’ll create a basic Spring Boot web application and add some useful services to it.

 How to set up the Springboot Webapp?

An initial setup for the project is one of the main challenges of starting a new project. We need to make a decision on the various directory layout and also make sure that we meet all industry guidelines. We need to use the resources to build a Spring Boot web application.

  • Our preferred IDE (I will be using Eclipse) 
  • Maven
  • JDK 1.8+

The project structure of SpringBoot webapp.

There are many ways to use the Spring Boot Initializr to create a project framework for you.

For the convenience of this article, we use the Spring Initializer web interface to create the Structure project.

The Spring Initializr Web interface of your browser and you will be faced with a wizard to start your setup.

To start with, you are expected to fill in some details in the web interface. you can also refer this post for better understanding

Spring Boot does not require any specific framework architecture or structure. We will still follow any of the best practices recommended by the Spring Boot Team, but the final structure would be driven by the project requirements.

pom.xml for Springboot Webapp

Let ‘s start looking at the pom.xml file to understand the setup of Spring Boot in more depth. I’m just going to discuss the Spring Boot changes linked to pom.xml. Here’s a pom.xml file from our Springboot webapp project.

<?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.3.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.onurdesk</groupId>
	<artifactId>webapp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>webapp</name>
	<description>Demo project for Spring Boot webapp onurdesk</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<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>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

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

</project>

One of the key features of the Spring Boot is the “Starters,” a simple way to add the necessary dependencies (jars) to our class route. When using Spring Boot, we don’t have to add jar/dependencies to our classpath (in case the starter is not usable, you would need to add these dependencies to pom.xml or build your own custom starter). We just need to add the right “Starters” to our pom.xml format, and Spring Boot will make sure that these dependencies are automatically applied.

 Main Application for SpringBoot webapp

Here’s our main Spring Boot application class, this is the Spring Initialization class. The @SpringBootApplication annotation allows the Spring Context and all the initialization magic of the Spring Boot.

package com.onurdesk.webapp;

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

@SpringBootApplication
public class WebappApplication {

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

}

Lets understand of all annotation one by one of our springboot webapp :

@SpringBootApplication Annotation

@SpringBootApplication is similar to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default values. Once you launch your project, an annotation is recommended.

Using @SpringBootApplication in the main class is similar to following three annotations.

  • @Configuration is a bean description source
  • @EnableAutoConfiguration This gives Spring Boot an understanding of how to configure the program.
  • @ComponentScan will automatically pick up all Spring parts, including @Configuration classes.

Main Method

The main method is another important aspect of our main class. This is a standard approach that meets the standard Java workflow. Our key class can shift authority to the Spring Boot SpringApplication class.

The SpringApplication Class run method will be used for the BootStrap application.

Welcome Controller for springboot webapp

Last part of our configuration, we will create a welcome controller that will be responsible for managing GET requests for /hello by returning the View name, in this case, “welcome.” A View is responsible for the rendering of HTML content.

package com.onurdesk.controller;

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

@RestController
public class WelcomeController {

	@RequestMapping("/welcome")
	public String welcome() {

		return "Welcome to Springboot webapp ";
	}

}

This is a really basic controller, but it’s covered a number of points in our  Springboot webapp setup.

  • @RestController annotation indicates that an annotated class is a “Controller” (e.g. a web controller).
  • @RequestMapping annotation specifies that HTTP requests for /welcome are mapped to the welcome() method.
  • We did not define any method in the @RequestMapping annotation as default maps for all HTTP operations.
  • As we use Thymeleaf for displaying technologies and returning “welcome” from the welcome() method, Thymeleaf scans the welcome.html template and produces output.

UI Template 

Here’s our basic HTML prototype for Thymeleaf.

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>

</head>
<body>
Hello and Welcome to our Springboot webapp
</body>
</html>

When using Thymeleaf as our template engine, Spring Boot can scan for tools by encircling the view name with a prefix and suffix (externalized to spring.thymeleaf.prefix and spring.thymeleaf.suffix, defaults to ‘classpath:/templates/’ and ‘.html’ respectively).

Finally Run our Springboot webapp

We’re done with our Springboot webapp, it’s time to run our application. While this program can be packaged as a standard WAR file for deployment to an external web server, the simplified solution illustrated in the development of a standalone application. To run our IDE program, we need to run our web application as a standalone Java application.

With Maven, we can run the application using the command mvn spring-boot: run.
We can create a JAR file with mvn clean package command and run jar using java-jar target/webapp-0.1.0.jar.

Now that the site is up and running, please visit http:/localhost:8080/welcome and if all is in order, you should have the following output on your web browser.

Hello and Welcome to our Springboot webapp

Logs:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.5.RELEASE)

2020-10-31 20:48:04.884  INFO 17120 --- [           main] com.onurdesk.webapp.WebappApplication    : Starting WebappApplication on AIPL2646LT07134 with PID 17120 (D:\LearningNewTech\grey\Spring boot\webapp\target\classes started by mingle in D:\LearningNewTech\grey\Spring boot\webapp)
2020-10-31 20:48:04.891  INFO 17120 --- [           main] com.onurdesk.webapp.WebappApplication    : No active profile set, falling back to default profiles: default
2020-10-31 20:48:08.107  INFO 17120 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-10-31 20:48:08.135  INFO 17120 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-10-31 20:48:08.136  INFO 17120 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.39]
2020-10-31 20:48:08.324  INFO 17120 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-10-31 20:48:08.325  INFO 17120 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3304 ms
2020-10-31 20:48:08.687  INFO 17120 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-31 20:48:09.077  INFO 17120 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-10-31 20:48:09.092  INFO 17120 --- [           main] com.onurdesk.webapp.WebappApplication    : Started WebappApplication in 5.204 seconds (JVM running for 6.313)

Summary

In this article, we learned how to build a Spring Boot Web Application. Spring Boot comes with a lot of builds in the function to render and run a web application quickly and with limited effort.

Exit mobile version