2
votes

Created a spring boot REST application.

Run as Spring boot app
While running it as spring boot application in eclipse. It's working great.
REST API are working with the URL - http://localhost:8080/schools

Deploy as WAR on tomcat

I followed the following steps

  1. Main class extends SpringServletContainerInitializer
    @SpringBootApplication
    public class RmsBackendApplication extends SpringServletContainerInitializer {

        public static void main(String[] args) {
            SpringApplication.run(RmsBackendApplication.class, args);
        }
    }
  1. Added start class entry in pom.xml
    <properties>
        <java.version>1.8</java.version>
        <start-class>com.codingParkFun.rmsbackend.RmsBackendApplication</start-class>
    </properties>
  1. Added starter tomcat dependency
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
  1. Few documents say override configure method of SpringServletContainerInitializer. But in my case SpringServletContainerInitializer doesn't have a configure method. I downloaded spring tools 4.0

  2. Generated package with maven install. War package is generated with the name rms-0.0.1-SNAPSHOT.war

  3. Deployed the war package on the tomcat (version 9.0.27 )

  4. Accessing REST API URL - http://localhost:8080/rms-0.0.1-SNAPSHOT/schools

Output enter image description here

  1. Tomcat localhost logs are as follow :
09-Nov-2019 11:13:31.013 INFO [main] org.apache.catalina.core.ApplicationContext.log 1 Spring WebApplicationInitializers detected on classpath
09-Nov-2019 11:13:32.048 INFO [main] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
09-Nov-2019 11:13:32.048 INFO [main] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()
09-Nov-2019 11:13:32.052 INFO [main] org.apache.catalina.core.ApplicationContext.log ContextListener: attributeAdded('StockTicker', 'async.Stockticker@6231bb88')
09-Nov-2019 11:16:44.566 INFO [Thread-4] org.apache.catalina.core.ApplicationContext.log SessionListener: contextDestroyed()
09-Nov-2019 11:16:44.566 INFO [Thread-4] org.apache.catalina.core.ApplicationContext.log ContextListener: contextDestroyed()
09-Nov-2019 11:19:59.676 INFO [main] org.apache.catalina.core.ApplicationContext.log 1 Spring WebApplicationInitializers detected on classpath
09-Nov-2019 11:20:12.722 INFO [main] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
09-Nov-2019 11:20:12.723 INFO [main] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()
09-Nov-2019 11:20:12.728 INFO [main] org.apache.catalina.core.ApplicationContext.log ContextListener: attributeAdded('StockTicker', 'async.Stockticker@2420e962')
09-Nov-2019 11:27:44.515 INFO [main] org.apache.catalina.core.ApplicationContext.log 1 Spring WebApplicationInitializers detected on classpath
09-Nov-2019 11:27:45.490 INFO [main] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
09-Nov-2019 11:27:45.490 INFO [main] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()
09-Nov-2019 11:27:45.494 INFO [main] org.apache.catalina.core.ApplicationContext.log ContextListener: attributeAdded('StockTicker', 'async.Stockticker@1c7da28')
09-Nov-2019 11:38:48.705 INFO [http-nio-8080-exec-7] org.apache.catalina.core.ApplicationContext.log 1 Spring WebApplicationInitializers detected on classpath

Not sure while the application REST API is not working. Please guide.

1

1 Answers

6
votes

You need to extend SpringBootServletInitializer, not SpringServletContainerInitializer. Then you can override configure method as you say in step 4, like this:

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

Hope it helps!