Create a web application in 5 minutes using Spring Boot and Maven

Create a simple web application using Maven and Spring Boot

Sometime to do an R&D, we all need a sample web application to experiment and play around. 
Below are the steps which will help to create a web application quickly using Spring Boot and Maven.
  1. Create a maven project from your IDE in this case I am using Eclipse IDE.
    • Go to File > New > Project and then select Maven > Maven Project                                 







  2. Add spring boot dependencies into the pom as shown
    • pom.xml

  3. Add Controller and View (index.html) is the shown project structure
    • Add SimpleWebController.java, Application.java and index.html as shown below             




  4. Build the project and run it
    • use the command to run it mvn spring-boot:run
    • Once the application is launch successfully within the Embedded tomcat. Browse the application using http://localhost:8080                                                                               


Source Code:
pom.xml

<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.mycompany</groupId>
  <artifactId>simpleweb</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <description>Simple web Application</description>
  
  <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    
    <dependencies>
    <!-- Add typical dependencies for a web application -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
</project>

SimpleWebController.java

package com.mycompany.simpleweb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SimpleWebController {

@RequestMapping(value = "/")
public String index() {
return "index.html";
}
}

Application.java
package com.mycompany.simpleweb;

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

@SpringBootApplication
public class Application {

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


index.html

<!DOCTYPE html>
<html>
<head>
    <title>Simple Web Application</title>
</head>
<body>
<h1>Awesome, Simple Web Application is Ready !!!</h1>
</body>
</html>

Reference:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#getting-started-maven-installation



Comments

Post a Comment

Popular posts from this blog

Implementation of Documentum Business Object Framework (BOF)

Creating Session in Documentum