gpt4 book ai didi

java - Spring Boot boot在Java 6上运行

转载 作者:行者123 更新时间:2023-12-02 12:01:41 25 4
gpt4 key购买 nike

我在 Spring Boot 1.5.8 上使用 Jetty 自动配置器时遇到问题。我需要使用 Jetty 8 而不是 Jetty 9 来实现 Java 6 兼容性,但自动配置器未检测到 jetty 类:

   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"
}
}

这随后导致:

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
}
}

最佳答案

默认 Spring Boot 1.5 requires Java 7 或更高版本才能运行。然而,可以在 Java 6 上运行 Spring Boot 1.5,这在 the reference guide 中有很好的记录。 .

就您而言,您想得太多了,就像指定所需的 Jetty 版本一样简单。

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'
}
}

这样 Spring Boot 将包含正确的版本,而您无需自己包含它们。

关于java - Spring Boot boot在Java 6上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47199561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com