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.

Search the Web

Custom Search

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