Your First Spring Boot Web Application

In the previous example, we created a simple standalone application using the dependency spring-boot-starter. In this example, we will convert it into a simple web application with very minimal changes.

A Spring Boot application uses the @SpringBootApplication annotation over a class with a main method that will execute the application. The SpringApplication.run method call accepts two parameters — the class that actually contains the annotated @SpringBootApplication annotation and any application arguments. The ApplicationArguments interface allows you to access any application arguments. Spring Boot provides easy to use starter pom dependencies such as spring-boot-starter, spring-boot-starter-test, spring-boot-start-web.

 

Bean Components

We will use a simple bean class based on our previous examples. 

Within the bean file, we will use the annotation @RestController and @RequestMapping to expose the class as a service and to map to a url pattern. We will also change the signature of the bean's method to return a string that will be displayed in the browser.

JJWriter.java

package com.javajee.springboot;

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

@RestController
class JJWriter {
    
    @RequestMapping("/")
    public String write() {
        return "<h2>Inside JJWriter's method write()</h2>";
    }
}

 

Test Class (Main class)

A Spring Boot application can be created using the annotation @SpringBootApplication.

We will cleanup the JJWriterMain method from the previous example and it will only have the SpringApplication.run method call.

JJWriterMain.java

package com.javajee.springboot;

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

@SpringBootApplication
public class JJWriterMain {

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

Before executing the program, you must have also configured your Gradle/Maven dependencies.

 

Maven POM File

You need to add Spring Boot dependencies instead of Spring dependencies. Spring Boot provides easy to use starter pom dependencies such as spring-boot-starter, spring-boot-starter-test, spring-boot-start-web.

Only thing you will need to change in the pom.xml from the previous example is to change spring-boot-starter with spring-boot-start-web.

<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.javajee.springbootdemo</groupId>
  <artifactId>springbootdemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springbootdemo</name>
  <description>springbootdemo</description>
  
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

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

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

 

Executing and verifying the program

You can execute the main class and verify the program by running http://localhost:8080/ in the browser.

 

Application.properties file

You can create a file called application.properties within the resources folder and Spring Boot will automatically pick it up. Please note that this file is not required for the current example, unless you need to change the default port for the embedded tomcat.

You can change the default port of the embedded tomcat server by adding below line to the application.properties file.

server.port = 8090

Now you can execute the main class and verify the program by running http://localhost:8090/ in the browser.

This may be required in case of any port conflicts or simply as a personal choice.

Comments

You did really good work. I really appreciate your new and different post. Please guys keep it up and share with us some unique post in the future…
iMac service center in Delhi
ipad service center in Kolkata

Was it useful?

Wishing you the best of luck for all your blogging efforts.This is my first opportunity to chat this website. I found some interesting things and I will apply to the development of my blog.
Engineering college in punjab
Engineering college in Chandigarh

Was it useful?

SCS is focused on providing the most comprehensive, heterogeneous, integrated IT Software solutions to enterprises around the world.
Two way SMS Gateway provider
Lakeside software
AMC Services

Was it useful?

I think the source code which you have provided here would be useful to all students who are studying java and doing projects in java. I have executed this source code and it worked successfully. Thank for sharing this. Expecting more useful codes like this. don julio real

Was it useful?

Solutions Middle East, works to Envision, Execute and Empower our clients by offerings design, implementation and on-going support services.
Messaging Solutions
Enterprise Data center
Business intelligence analytics for IT
IT Business Tracking System in UAE
Citrix Platinum Partner

Was it useful?

You have shared the complete details of Suntandy Chopin Piano. I think this post is very much enough to know the proper address and contact details of the branches. I am expecting more posts on this blog. Keep updating! ipad 2 wholesale

Was it useful?

Java is a very complicated subject for me. I am a regular visitor to your site to learn more about Java. Thanks for sharing such informative dish network channels article s with step step info about programming in Java. I appreciate your efforts. Thanks...

Was it useful?

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!