Spring boot traditional war deployment
Spring Boot supports traditional deployment as well as more modern forms of deployment, Assume if you need a spring boot application to be deployed in existing container or server like weblogic or webspear, you need traditional war for deployment, follow below steps to create it.
Change packing type to war
Update your build configuration to use war as packing instead of jar, If you use Maven and spring-boot-starter-parent (which configures Maven’s war plugin for you), all you need to do is to modify pom.xml to change the packaging to war as below
Change tomcat to provided state
Now mark the embedded servlet container dependency as provided to produce an executable war file, Since the tomcat is in the provided state, you can also run your application by using java -jar on the command line.
Complete pom.xml
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.candidjava.spring.boot</groupId> <artifactId>helloworld-war</artifactId> <version>0.0.1-SNAPSHOT</version> <name>helloworld-war</name> <url>http://maven.apache.org</url> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Configuring Spring boot application
The last step is to configure your Spring boot application, extend SpringBootServletInitializer in your Spring boot Main class, and override its main method. Doing so makes use of Spring Framework’s Servlet 3.0 support and lets you configure your application when it is launched by the servlet container.
package com.candidjava.spring.boot.helloworld; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class Example extends SpringBootServletInitializer { public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Example.class); } }
Sample rest controller
package com.candidjava.spring.boot.helloworld; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class RestController { @RequestMapping("/user") String home() { return "Hello"; } }
Download
Download source code from my github account Click here