Deployment of Spring Boot in external tomcat

Learn Deployment Of Spring Boot in External Tomcat

In this example we will use same project that we created earlier.To create project you can follow below link.

REST API Example Using Spring Boot


Or you can download above project from given link.

Spring Boot REST Example


To test deployment we are using following class which contains a GET API

 
package com.example.preparationforinterview.FirstSpringBootPreparation;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestService {	
	
	@RequestMapping(value = "/api", method = RequestMethod.GET)
	public String getMessage(@RequestParam("name") String name){
		return "Hello" + " " + name;		
	}
}

If we have RestService on separate package then we need to include @Import({ RestService.class }) in spring boot class , in our example


 
@SpringBootApplication
@Import({ RestService.class })
public class FirstSpringBootPreparationApplication {

By default spring boot project generate jar in target folder if you are using maven. To deploy spring boot application we will create war from application.To generate war from spring boot ,go to pom.xml file and add

<packaging>war</packaging>

After mvn install ,it will generate war in target folder.In our case it will be FirstSpringBootPreparation-0.0.1-SNAPSHOT.war

If we deploy this war into external tomcat we can get error because of tomcat conflict so to remove our embedded tomcat,we will modify our web starter as below to exclude tomcat.
 
	<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>

It will deploy in tomcat but it will not hit any url unless it includes a web.xml file or a servlet initializer to make enable Spring MVC DispatcherServlet.Spring boot provides SpringBootServletInitializer that is an implementation of WebApplicationInitializer. WebApplicationInitializer. Configures DispatcherServlet.

To create SpringBootServletInitializer , we will create subclass and override configure() method as below.

 
package com.example.preparationforinterview.FirstSpringBootPreparation;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class MyServletInitializer extends SpringBootServletInitializer {
	@Override
	protected SpringApplicationBuilder configure(
			SpringApplicationBuilder builder) {
		return builder.sources(FirstSpringBootPreparationApplication.class);
	}
}

Add one more dependency to put javax.servlet on classpath

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

After mvn install ,it will generate war in target folder.In our case it will be FirstSpringBootPreparation-0.0.1-SNAPSHOT.war

Now put war in webapps folder of tomcat and start tomcat.It will start tomcat.

Now go to below url

REST End Point URL

It will display as below



Download source code

Deployment of Spring Boot In External Tomcat



Visit Others Links

Spring Boot Introduction
Spring Boot Setup
REST API Example Using Spring Boot
Spring Boot REST API Test with Mock MVC Using standaloneSetup
Spring Boot REST API Test with Mock MVC Using webAppContextSetup
Spring Boot REST API Test with Embedded Server
Actuator In Spring Boot
Deployment Of Spring Boot In External Tomcat
Spring Boot with OAuth
Top Spring Boot Interview Question