Previously we have learnt how to build a Spring Boot Hello World application using Maven and through Eclipse. In this Spring Boot example, we will be building the same hello world application with a slight difference instead of building an executable JAR we will be building a WAR file. We will be extending SpringBootServletInitializer in order to create deployable WAR file.
How to Create Deployable WAR
Folder Structure:
- Create a simple Maven Project “SpringBootTutorial” by selecting maven-archetype-quickstart and create a package for our source files “com.javainterviewpoint” under src/main/java
- Create the Java classes HelloWorld.java under com.javainterviewpoint folder.
- Place the POM.xml in the root directory
Step 1: Extend SpringBootServletInitializer class
Place HelloWorld.java under com.javainterviewpoint folder. We will be extending SpringBootServletInitializer and will be will overriding it’s configure() method. SpringBootServletInitializer is an abstract class implementing WebApplicationInitializer Interface.
package com.javainterviewpoint; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class HelloWorld extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(HelloWorld.class); } public static void main(String[] args) throws Exception { SpringApplication.run(HelloWorld.class, args); } @RequestMapping("/") String hello() { return "Hello World! JavaInterviewPoint888"; } }
We have added the below annotations in our HelloWorld class
- @RestController – This annotation is a stereotype annotation, this annotation tells spring to render the result back to the caller.
- @RequestMapping – This annotation will any HTTP request with the path “/” should be mapped to the hello() method
- @EnableAutoConfiguration – This annotation tells the Spring Boot to configure the application based on the dependencies added. Since spring-boot-starter-web has added Tomcat and Spring MVC, auto-configuration will setup a web based application.
Our main() method is the triggering point of our java application, it in-turn calls Spring Boot’s SpringApplication class run() method which bootstrap our HelloWorld application. We need to pass our HelloWorld.class as an argument to our run() method.
Step 2: Packaging to WAR
Since we are using Maven and spring-boot-starter-parent (Configures Maven WAR), all we need to do is that add <packaging> tag as WAR in our POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.javainterviewpoint</groupId>
<artifactId>SpringBootTutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
.......
Step 3: Exclude Spring Boot Embedded Tomcat
As we will be deploying our WAR file to a external container, we doesn’t want the embedded tomcat to interfere with the External Servlet Container where our WAR file will be deployed. We just need to mark the embedded container dependency as ‘provided’
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Finally, the updated POM will be
<?xml version="1.0" encoding="UTF-8"?> <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.javainterviewpoint</groupId> <artifactId>SpringBootTutorial</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <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>
Step 4: Creating Deployable WAR
Now we are good for creating the executable WAR file. In STS / Eclipse, Right click on the Project –>Run As –> Run Configuration–> Give goals as “clean package”. If you are running through Maven command prompt then WAR can be build using the command “mvn clean package”. We will be having the WAR file created in the Target folder
Copy the WAR (SpringBootTutorial-0.0.1-SNAPSHOT.war) and put it in the /webapps of the external container(Tomcat) and start the server (/bin/startup.bat)
Console
INFO: Deploying web application archive C:\apache-tomcat-7.0.53\webapps\SpringBootTutorial-0.0.1-SNAPSHOT.war . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.1.RELEASE) 2017-03-13 14:32:07.993 INFO 7784 --- [ost-startStop-1] com.javainterviewpoint.HelloWorld : Starting HelloWorld on DA56CZ8VD02 with PID 7784 ( C:\Jackson\apache-tomcat-7.0.53\webapps\SpringBootTutorial-0.0.1-SNAPSHOT\WEB-INF\classes\com\javainterviewpoint\HelloWorld.class started by xbbl47m i n C:\Jackson\apache-tomcat-7.0.53\bin) 2017-03-13 14:32:08.000 INFO 7784 --- [ost-startStop-1] com.javainterviewpoint.HelloWorld : No active profile set, falling back to default pro files: default 2017-03-13 14:32:08.075 INFO 7784 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedd ed.AnnotationConfigEmbeddedWebApplicationContext@46978e24: startup date [Mon Mar 13 14:32:08 IST 2017]; root of context hierarchy 2017-03-13 14:32:09.035 INFO 7784 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.valid ation.ValidationAutoConfiguration' of type [class org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration] is not eligible for g etting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2017-03-13 14:32:09.141 INFO 7784 --- [ost-startStop-1] trationDelegate$BeanPostProcessorChecker : Bean 'validator' of type [class org.springframewor k.validation.beanvalidation.LocalValidatorFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2017-03-13 14:32:09.174 INFO 7784 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization complet ed in 1100 ms 2017-03-13 14:32:09.629 INFO 7784 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2017-03-13 14:32:09.630 INFO 7784 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'errorPageFilter' to: [/*] 2017-03-13 14:32:09.631 INFO 7784 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2017-03-13 14:32:09.631 INFO 7784 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2017-03-13 14:32:09.632 INFO 7784 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/* ] 2017-03-13 14:32:09.635 INFO 7784 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2017-03-13 14:32:10.058 INFO 7784 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework .boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@46978e24: startup date [Mon Mar 13 14:32:08 IST 2017]; root of context hierarchy 2017-03-13 14:32:10.146 INFO 7784 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.javainter viewpoint.HelloWorld.hello() 2017-03-13 14:32:10.151 INFO 7784 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframewor k.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.s ervlet.http.HttpServletRequest) 2017-03-13 14:32:10.152 INFO 7784 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto publ ic org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServl etRequest,javax.servlet.http.HttpServletResponse) 2017-03-13 14:32:10.196 INFO 7784 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-03-13 14:32:10.196 INFO 7784 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-03-13 14:32:10.251 INFO 7784 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-03-13 14:32:10.514 INFO 7784 --- [ost-startStop-1] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2017-03-13 14:32:10.535 INFO 7784 --- [ost-startStop-1] com.javainterviewpoint.HelloWorld : Started HelloWorld in 3.122 seconds (JVM running f or 7.925) Mar 13, 2017 2:32:11 PM org.apache.catalina.startup.Catalina start INFO: Server startup in 7957 ms
Output :
Hit on the URL “http://localhost:8080/SpringBootTutorial-0.0.1-SNAPSHOT/”
Leave a Reply