0
votes

I'm having trouble with the autoconfigurer for Jetty on Spring Boot 1.5.8. I need to use Jetty 8 instead of Jetty 9 for Java 6 compatibility, but the autoconfigurer does not detect the jetty classes:

   EmbeddedServletContainerAutoConfiguration.EmbeddedJetty:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.eclipse.jetty.webapp.WebAppContext' (OnClassCondition)

The dependencies part of my build.gradle:

dependencies {
    compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.12'
    compile 'org.springframework.boot:spring-boot-starter-web', {
        exclude module: 'spring-boot-starter-tomcat'
        exclude group: 'com.fasterxml.jackson.core'
    }

    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.7.9' // last version for Java 6
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.7.9'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.7.9.1'

    providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
    //providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'

    providedRuntime 'org.springframework.boot:spring-boot-starter-jetty', {
        exclude group: 'org.eclipse.jetty'
        exclude group: 'org.eclipse.jetty.websocket'
    }
    def JETTY8_VERSION = '8.1.22.v20160922'
    ['jetty-server', 'jetty-webapp', 'jetty-servlets', 'jetty-continuation', 'jetty-client',
            'jetty-http', 'jetty-util', 'jetty-io', 'jetty-servlet', 'jetty-xml', 'jetty-security'].each {
        providedRuntime "org.eclipse.jetty:$it:$JETTY8_VERSION"
    }
}

This subsequently results in:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    ...

The strange part is, if I declare EmbeddedServletContainerFactoryBean explicitly, it works:

@Bean
EmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
    try {
        def clazz = Class.forName('org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory')
        clazz.newInstance()
    } catch (NoClassDefFoundError cnfe) {
        null
    }
}
1
Which spring boot version. Also why the complex addition of jetty? Just set the proper version. Set ext['jetty.version'] = '8.1.22.v20160922' instead what you have now. The same applies to Jackson ext['jackson.version] = '2.7.9'`. - M. Deinum
@M.Deinum It's 1.5.8. And I did it that way because I didn't know any better. - Artefacto
Spring Boot 1.5 requires by default Java 7, you need to do additional configuration for Java 6 which is quite nicely explained in the reference guide. - M. Deinum
@M.Deinum Changing the dependency section to using ext from what I had before was enough for the autoconfigurer to work (though I'm not sure exactly why). If you want to write that up as an answer, I'll accept it. - Artefacto

1 Answers

1
votes

By default Spring Boot 1.5 requires Java 7 or higher to run. However it is possible to run Spring Boot 1.5 on Java 6 and that is quite nicely documented in the reference guide.

In your case you are overthink things it is as simply as specifying the desired Jetty version.

ext['jetty.version'] = '8.1.22.v20160922'

dependencies {
    compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.12'
    compile 'org.springframework.boot:spring-boot-starter-web', {
        exclude module: 'spring-boot-starter-tomcat'
        exclude group: 'com.fasterxml.jackson.core'
    }

    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.7.9' // last version for Java 6
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.7.9'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.7.9.1'

    providedCompile 'javax.servlet:javax.servlet-api:3.0.1'

    providedRuntime 'org.springframework.boot:spring-boot-starter-jetty', {
        exclude group: 'org.eclipse.jetty.websocket'
    }
}

This way Spring Boot will include the proper versions and you don't need to include them yourself.