user3290940
user3290940

Reputation: 61

LifecycleException deploying Spring Boot WebSocket example into Tomcat

After successfully running through the Spring getting started guide for WebSocket, I want desperately to build this application as a WAR and deploy it into Tomcat. I have done my best to modify the project to build as a WAR file by following the Spring getting started guide for converting a Spring Boot JAR to a WAR, but when I deploy the resulting WAR, Tomcat throws an exception.

I'm developing using STS 3.4.0 with Gradle 1.10 (I switched to this after previously failing to successfully deploy when building with Maven in Eclipse). Gradle reports a successfully built WAR file, but when I attempt to deploy it into Tomcat, the following exception is thrown:

09-Feb-2014 17:30:27.537 SEVERE [localhost-startStop-3] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: 
 org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/gs-messaging-stomp-websocket-0.1.0]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:702)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:699)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:977)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1763)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/websocket/WebSocketAutoConfiguration$TomcatWebSocketConfiguration.class]: Initialization of bean failed; nested exception is java.lang.ArrayStoreException: org.apache.catalina.valves.RemoteIpValve
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:124)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:658)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:355)
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:129)
    at org.springframework.boot.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:90)
    at org.springframework.boot.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:53)
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:181)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5237)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 10 more
Caused by: java.lang.ArrayStoreException: org.apache.catalina.valves.RemoteIpValve
    at org.springframework.boot.autoconfigure.web.ServerProperties$Tomcat.customizeTomcat(ServerProperties.java:212)
    at org.springframework.boot.autoconfigure.web.ServerProperties.customize(ServerProperties.java:117)
    at org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:68)
    at org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor.postProcessBeforeInitialization(EmbeddedServletContainerCustomizerBeanPostProcessor.java:54)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    ... 27 more

I'm running Tomcat 8.0.0-RC10 on Windows 8.1 64-bit. I believe the relevant application files are these:

Application.java:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

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

HelloWebXml.java (in lieu of an actual web.xml):

package hello;

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

public class HelloWebXml extends SpringBootServletInitializer {

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

}

and finally, build.gradle:

buildscript {
    repositories {
        maven { url "http://repo.spring.io/libs-snapshot" }
        mavenLocal()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC1")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'

war {
    baseName = 'gs-messaging-stomp-websocket'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-snapshot" }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.BUILD-SNAPSHOT")
    compile("org.springframework.boot:spring-boot-starter-websocket:0.5.0.BUILD-SNAPSHOT")
    compile("org.springframework:spring-messaging:4.0.0.RELEASE")
    compile("com.fasterxml.jackson.core:jackson-databind")
    testCompile("junit:junit:4.11")
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.10'
}

I must say, I find it ironic and more than a little frustrating that the problem seem to lie with the tomcatEmbeddedServletContainerFactory. Thus a feature whose sole purpose (I believe) is to allow the app to be run OUTSIDE of a servlet container --- a feature in which I have absolutely no interest --- is preventing me from properly deploying an app INTO a servlet container.

All help will be appreciated.

Upvotes: 2

Views: 3794

Answers (2)

user3290940
user3290940

Reputation: 61

With the help of Mr. Syer's answer (sorry, I tried to upvote, but I'm too new to this forum), along with information from this post about how to mark a resource as 'provided', I finally got this building as a war which I can successfully deploy in Tomcat.

After a bit of experimentation, it appears that what is needed is to make the following changes to my build.gradle:

configurations {
    provided
}

sourceSets {
    main.compileClasspath += configurations.provided
    test.compileClasspath += configurations.provided
    test.runtimeClasspath += configurations.provided
}

eclipse.classpath.plusConfigurations += configurations.provided 

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:0.5.0.BUILD-SNAPSHOT") {
        exclude module: 'spring-boot-starter-tomcat'
    }
    compile("org.springframework.boot:spring-boot-starter-websocket:0.5.0.BUILD-SNAPSHOT") {
        exclude module: 'spring-boot-starter-tomcat'
    }   

    provided group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: '0.5.0.BUILD-SNAPSHOT'

    compile("org.springframework:spring-messaging:4.0.0.RELEASE")
    compile("com.fasterxml.jackson.core:jackson-databind")
    testCompile("junit:junit:4.11")
}

When I applied only the 'exclude module' changes, I got a compile-time error complaining that "class file for javax.servlet.ServletException not found" --- which confuses me, since I believe that is part of my Java 7 JDK.

When I applied only the 'provided group' change, it didn't actually remove the spring-boot-starter-tomcat jar(s) from my war.

Upvotes: 4

Dave Syer
Dave Syer

Reputation: 58104

You need to make the embedded tomcat dependency "provided" (right now you have spring-boot-starter-tomcat in the "compile" configuration, transitively through spring-boot-starter-web). If you really have no use for an embedded server you can remove it completely. Gradle has standard idioms for excluding transitive dependencies, and for adding them in different configurations, I believe.

Upvotes: 1

Related Questions