Spring Initializer and simple Web Spring Boot application

Spring Initializer and simple Web Spring Boot application
Photo by Anisur Rahman on Unsplash

In this post I will show you how you can start your adventure with Spring Boot. We will create a new project using “Spring Initializer” and implement a simple controller to display “Hello world from web Spring Boot!” in your browser.

Craete new project

To create a ready project based on Maven or Gradle, just go to the website Spring Initializer.

Select:

  • Project: Maven
  • Project Metadata Group: com.bigdataetl
  • Project Metadata Artifact: webdemo
  • Dependencies: Spring Web

Then click the Genrate button and after a while we will have a ready package of webdemo.zip to download. We download the file, unpack and open it in our favorite IDE, e.g. IntelliJ IDEA.

Module overview

Let’s take a look at the design tree first. Spring Boot has created for us all the necessary directories and classes to be able to start working as soon as possible, and not to create directories.

As we can see in the picture below we have created:

  • Main class: WebdemoApplication.class – we also see annotations @SpringBootApplication
  • The resources directory and in it ready directories for our html page patterns, css or js files. In addition, we see the application.properties file that will be used to parameterize our application.
  • The test catalog with ready test class for our main application.
package com.bigdataetl.webdemo;

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

@SpringBootApplication
public class WebdemoApplication {

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

}

Controller

We will now create a class that will be responsible for handling the request at http://localhost:8080/hello

To do this, create a new com.bigdataetl.controller package and the HelloController.class class inside this package.

package com.bigdataetl.webdemo.controller;

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

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String helloWorldFromSpringBoot() {
        return "Hello World from Spring Boot!";
    }

}

Run the application

Now just run our main class or WebdemoApplication.class. In the logs you will see how Spring Boot automatically configured the Apache Tomcat service for you on port 8080.

o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-03-01 22:24:35.845  INFO 8804 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]

Now just run the URL in your browser: http://localhost:8080/hello and you’ll see that you get what the helloWorldFromSpringBoot() method returns to us:

    @RequestMapping("/hello")
    public String helloWorldFromSpringBoot() {
        return "Hello World from Spring Boot!";
    }

Below is the full log of the launch:

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

2020-03-01 22:24:34.706  INFO 8804 --- [           main] c.bigdataetl.webdemo.WebdemoApplication  : Starting WebdemoApplication on pawel with PID 8804 (/home/pawel/Desktop/Blog/webdemo/target/classes started by pawel in /home/pawel/Desktop/Blog/webdemo)
2020-03-01 22:24:34.713  INFO 8804 --- [           main] c.bigdataetl.webdemo.WebdemoApplication  : No active profile set, falling back to default profiles: default
2020-03-01 22:24:35.832  INFO 8804 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-03-01 22:24:35.845  INFO 8804 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-01 22:24:35.845  INFO 8804 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-01 22:24:35.911  INFO 8804 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-01 22:24:35.912  INFO 8804 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1111 ms
2020-03-01 22:24:36.118  INFO 8804 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-01 22:24:36.260  INFO 8804 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-03-01 22:24:36.265  INFO 8804 --- [           main] c.bigdataetl.webdemo.WebdemoApplication  : Started WebdemoApplication in 2.146 seconds (JVM running for 2.673)
2020-03-01 22:24:37.301  INFO 8804 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-03-01 22:24:37.302  INFO 8804 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-03-01 22:24:37.313  INFO 8804 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 10 ms

If you enjoyed this post please add the comment below or share this post on your Facebook, Twitter, LinkedIn or another social media webpage.
Thanks in advanced!

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments