How to deploy spring boot war in jboss

In this article, we will see what are different strategies to deploy spring boot applications. Spring Boot lets you focus mainly on application development without worrying about some common configurations you used to do in spring. You don’t even need to bother about deployment part while in development stage, thanks to its Embedded servlet container that comes along with spring-boot-starter-web dependency. Isn’t this interesting? We will start by developing a simple spring boot web application and then see how to deploy spring boot war in JBOSS server and some other application servers.

What you will learn??

  • Creating a simple Spring Boot application
  • Deploying to default embedded tomcat servlet container (tomcat 7)
  • Upgrading the embedded tomcat version to tomcat 8
  • Deploying to Jetty Embedded Servlet Container
  • Deploying to External tomcat server as a war
  • Deploying to jboss-eap-7.1 as a war

Tools & Technologies Used

Getting started with Spring Boot

There are different ways you can start creating applications in spring boot. You can use Spring Initializr web service at https://start.spring.io/ and fill all the required information to generate spring boot application. Or you can use your favorite IDE like IntelliJ IDEA or Eclipse or Pivotal provided STS-Spring Tool Suite. Its just a matter of choice. I will be using STS which is Eclipse-based development IDE that is customized for developing Spring applications. Now let us start the development.

Open STS IDE-> File menu -> New -> Spring Starter Project and fill the info as following.

Click on Next button and choose spring boot version, web dependency and hit Finish button.

HelloSpringBoot project will be created and we will customize the package structure and add few classes as the following.

What’s inside pom.xml ?

You will get the following dependencies in 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.codeadda</groupId>
<artifactId>HelloSpringBoot</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>

<name>HelloSpringBoot</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Bootstrap your application

HelloSpringBootApplication class will bootstrap the spring boot application and starts an instance of default Embedded tomcat server. HelloSpringBoot app will automatically get deployed in this embedded tomcat server.

package com.codeadda.springboot;

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

@SpringBootApplication
public class HelloSpringBootApplication {

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

Lets create a simple Controller class with the name HelloController.java. Add @RestController annotation at class level. @RestController is often used for APIs that serve JSON, XML, etc. Your controller methods will return an object that will be serialized to one or more of these formats. Know more about various spring-boot annotations at pivotal site.

 

package com.codeadda.springboot.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {
@RequestMapping("/hello/{name}")
public String hello(@PathVariable String name) {
return "Howdy " + name + ", have a good day";
}
}

@RequestMapping will create a route so that our application will be accessible at /hello/{name}. hello(-) method accepts one parameter and simply returns greeting message. String name is a path variable mapped with {name}. 

 

That’s all you need to start your deployment. But in case, you want to deploy this application on some other ports (default is 8080), simply add server.port=8081 this key-value in application.properties file as shown in the following.

Embedded Tomcat Server Deployment

If you are a novice and just started learning spring boot, you might be wondering that how this embedded tomcat server will be automatically started and how deployment process will happen. Cool !, don’t bother about this. Spring Boot is smart enough to do it for you. When you run HelloSpringBootApplication either as Java Application or Spring Boot App, Spring Boot uses public static void main as entry-point that launches an embedded web server for you. So, you see, there is no magic at all. Now that you know the process, run the HelloSpringBoot application as Spring Boot App (or Java Application), default embedded tomcat server will be started on the port configured. You must see Tomcat started on port(s): 8081 (http) with context path ‘ ‘  in the console. Your wait is over, now just invoke the following URL in your favorite browser to see the greeting message.

http://localhost:8081/hello/AnshuBhai

By default, Spring boot uses Tomcat 7. But if you want to use tomcat 8, for this you need to override the maven build’s tomcat.version in the <properties> tag of pom.xml file as the following.

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<tomcat.version>8.0.3</tomcat.version>
</properties>

Jetty Embedded Servlet Container

Now that we worked with embedded tomcat server, lets try to use Jetty Embedded Servlet Container this time. Jetty project has been hosted at the Eclipse Foundation since 2009. In order to use the jetty server, first we have to exclude spring-boot-starter-tomcat module and add spring-boot-starter-jetty module. Here is the revised pom.xml file (only dependencies part).

 
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
 

Now, simply run the application and you will notice Jetty started on port(s) 8081 (http/1.1) with context path ‘/’  in the console.

Deploying As WAR file on Java EE Application Servers

So far we discussed on deploying spring boot web application on embedded servers but what about typical Java EE Application servers like jboss, weblogic or external tomcat server. Working with embedded server is fine as long as you are working in development environment. But what about deploying it as typical war file in UAT or Production environment. Well, in that case you have to make certain changes. Now i will show how to deploy spring boot application on external tomcat server and also on jboss-eap-7.1 server as war file.

Spring Boot WAR + External Tomcat Deployment

So far we worked with different embedded servlet containers. First of all, we will try deploying to external tomcat. In order to deploy spring boot app as war on tomcat, we need to make following changes.

  • Change the packaging from jar to war
  • Exclude the embedded server container or mark it as provided.
  • Add servlet api dependency
  • Extends from SpringBootServletInitializer

Change the packaging from jar to war

If you remember, while creating the project, we have selected the packaging as jar. So change the packaging to war in pom.xml file.

<packaging>war</packaging>

Exclude the embedded server container or mark it as provided

Since we are going to deploy war to external tomcat , there is no need of embedded servlet container. So, there are two things we can do. Either we can mark the spring-boot-starter-tomcat dependency as provided or exclude it. I believe exclusion will be a good idea as marking it as provided will increase the size of war file that we will generate for deployment. This will make war file few megabytes smaller in size. So exclude the servlet container from spring-boot-starter-web as following in pom.xml.


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

Add servlet api dependency

We have to add servlet api related dependency in pom.xml and mark its scope as provided as following.

 <dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency> 

The revised pom.xml after all changes should finally look like the following.


<?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.codeadda</groupId>
<artifactId>HelloSpringBoot</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>

<name>HelloSpringBoot</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

Extends from SpringBootServletInitializer

 

To set up the application as a servlet we have to extend the main class with SpringBootServletInitializer and override the configure method using SpringApplicationBuilder. After changes, HelloSpringBootApplication class looks like
the following.

package com.codeadda.springboot;

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 HelloSpringBootApplication extends SpringBootServletInitializer {

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

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(HelloSpringBootApplication.class);
}

}

 

Generate the war file on tomcat server

This is the final step. We will create a war file with the help of maven build. Right click on HelloSpringBoot -> Run As -> Maven Build.. -> mention clean package as Goals -> Hit the Run button.

This will create a war file HelloSpringBoot-0.0.1.war in target folder of projects directory. Copy this war file in webapps folder of your tomcat installation and start your server. You will be able to access the following url with your configured port of your external tomcat.

http://localhost:4545/HelloSpringBoot-0.0.1/hello/Mohit

Spring Boot WAR + JBOSS-EAP-7.1 Deployment

Now we will see how to deploy spring boot war in jboss-eap-7.1. I hope you have jboss-eap-7.1 installed on your machine. If not, get it downloaded from official redhat site. In order to download, you need to have account on redhat site. Its completely free to register.

https://developers.redhat.com/products/eap/download/

Once downloaded, extract the zip file in your preferred location. Before getting into deployment part, we have to do few changes with respect to Jboss server deployment. First of all add the below key in application.properties file.

server.servlet.path = /*

Second, we have to create jboss-web.xml that contains the context root configuration.

If your project directory does not already contain webapp/WEB-INF folder, create it and create a jboss-web.xml file with the following content.

<jboss-web>
<context-root>/SpringBoot</context-root>
</jboss-web>

Thats all you need to to. Now again generate war file with the help of maven build.

Once HelloSpringBoot-0.0.1.war is generated, copy this artifact to “jboss-eap-7.1.0\jboss-eap-7.1\standalone\deployments” directory. Double click on standalone.bat file to start the jboss server. If successfully deployed, HelloSpringBoot-0.0.1.war.deployed will be generated. Just make sure no other services are running on port 8080 as default port on which jboss server will be started is also 8080.

Now you will be able to access the spring boot app with the help of following url. Don’t be confused with SpringBoot in the url as it is the context-root we configured in jboss-web.xml file.

http://localhost:8080/SpringBoot/hello/Suryamani

That’s all in this article. I hope you will like this. In case of any query, please feel to write in the comment section. Kindly support us by subscribing our blog if not done already. Have a good day. Happy Coding.

References

https://spring.io/blog/2014/03/07/deploying-spring-boot-applications

https://stackoverflow.com/questions/37737129/deploying-spring-boot-on-jboss-eap-6-1

Share Knowledge
This entry was posted in Java, Spring Boot and tagged , . Bookmark the permalink.

10 Responses to How to deploy spring boot war in jboss

  1. Howdy! I simply want to give a huge thumbs up for the good information you may have right here on this post. I will probably be coming back to your weblog for more soon.

  2. anas says:

    This solution is not working for me

  3. Adoyi Abah Sunday says:

    my web context is still pointing to ‘/HelloSpringBoot-0.0.1-SNAPSHOT’ after i added jboss-web.xml

  4. Rishi Beohar says:

    I am still getting 404 found for the controller class.

  5. Pingback: Enable HTTPS with Self-Signed-Certificate in Spring Boot 2.0.2 - code-adda

  6. Avinash Anshu says:

    simple and best way to deploy the spring boot app through jboss.

Leave a Reply

Your email address will not be published. Required fields are marked *