Friday, 13 September 2019

SpringBootServletInitializer example

In traditional web application, we will generate a war file and deploy the war file in an application server like Tomcat. Similarly if you want to deploy the WAR file in traditional approach you can use SpringBootInitializer.

App.java
package com.sample.app;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@SpringBootApplication
public class App extends SpringBootServletInitializer {
 
 @Value("${appName}")
 private String appName;
 
 @Value("${version}")
 private String version;
 
 public static void main(String[] args) throws Exception {
  SpringApplication.run(App.class, args);
 }
 
 @RestController
 public class HelloController {
 
  @GetMapping("/")
  public String home() {
   return appName + " : " + version;
  }
 }
 
}
 
application.properties
appName=Chat Server
version=1.23

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>springBootInitDemo</groupId>
 <artifactId>springBootInitDemo</artifactId>
 <version>1</version>
 <packaging>war</packaging>
 
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
 </parent>
 
 <name>springbootApp</name>
 <url>http://maven.apache.org</url>
 
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 
 <dependencies>
 
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
 
 </dependencies>
 
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>
 
Run App.java.


Open the url ‘http://localhost:8080/’ in browser.

You can download complete working application from this link.



Previous                                                    Next                                                    Home

No comments:

Post a Comment