[Example] A Simple Spring Boot REST Web Application

A Spring Boot application uses the @SpringBootApplication annotation over the main executable class. The SpringApplication.run method call accepts two parameters — the configuration class annotated with @SpringBootApplication annotation and any application arguments. 

 

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.

 

Changing Port With Application.properties file

If you create a file called application.properties within the resources folder, Spring Boot will automatically pick it up as the default properties file.

For example, 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.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) Arrays (1) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (55) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (9) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (7) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)