org.springframework.boot.web.support 不存在
Posted
技术标签:
【中文标题】org.springframework.boot.web.support 不存在【英文标题】:org.springframework.boot.web.support does not exist 【发布时间】:2016-11-19 16:42:17 【问题描述】:我正在将构建系统从 maven 更改为 gradle 以用于 Spring Boot 项目。我得到了这个堆栈跟踪
19:03:08: Executing external task 'bootRun'...
/home/dac/proj/spring-boot-master/spring-boot-samples/spring-boot-sample-jetty-jsp/src/main/java/sample/jetty/jsp/SampleJettyJspApplication.java:22: error: package org.springframework.boot.web.support does not exist
import org.springframework.boot.web.support.SpringBootServletInitializer;
^
/home/dac/proj/spring-boot-master/spring-boot-samples/spring-boot-sample-jetty-jsp/src/main/java/sample/jetty/jsp/SampleJettyJspApplication.java:25: error: cannot find symbol
public class SampleJettyJspApplication extends SpringBootServletInitializer
^
symbol: class SpringBootServletInitializer
/home/dac/proj/spring-boot-master/spring-boot-samples/spring-boot-sample-jetty-jsp/src/main/java/sample/jetty/jsp/SampleJettyJspApplication.java:27: error: method does not override or implement a method from a supertype
@Override
^
3 errors
:compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 2.334 secs
Compilation failed; see the compiler error output for details.
19:03:10: External task execution finished 'bootRun'.
我的build.gradle
buildscript
repositories
mavenCentral()
dependencies
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.3.6.RELEASE"
classpath 'org.springframework:springloaded:1.2.5.RELEASE'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'rebel'
buildscript
repositories
mavenCentral()
dependencies
classpath group: 'org.zeroturnaround', name: 'gradle-jrebel-plugin', version: '1.1.3'
jar
baseName = 'gs-spring-boot'
version = '0.1.0'
repositories
mavenCentral()
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies
// tag::jetty[]
compile("org.springframework.boot:spring-boot-starter-web")
exclude module: "spring-boot-starter-tomcat"
compile("org.springframework.boot:spring-boot-starter-jetty")
// end::jetty[]
// tag::actuator[]
compile("org.springframework.boot:spring-boot-starter-actuator")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("junit:junit")
// change default IntelliJ output directory for compiling classes
idea
module
inheritOutputDirs = false
outputDir = file("$buildDir/classes/main/")
task wrapper(type: Wrapper)
gradleVersion = '2.3'
我的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-samples</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-sample-jetty-jsp</artifactId>
<packaging>war</packaging>
<name>Spring Boot Jetty JSP Sample</name>
<description>Spring Boot Jetty JSP Sample</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>$basedir/../..</main.basedir>
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>apache-jsp</artifactId>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
我应该如何使包可用于 gradle?
【问题讨论】:
为什么同时使用 maven 和 gradle?使用一个或另一个,而不是两者。 由于这是一个 Java 源文件的缺陷,一个聪明的 IDE 可能会准确地告诉您如何修复它:单击-单击-完成。 Visual Studio Code 为我解决了这个错误。 【参考方案1】:您正在使用 org.springframework.boot.context.web.SpringBootServletInitializer
这已被弃用。而是:
使用
org.springframework.boot.web.support.SpringBootServletInitializer
对于 SpringBoot 2.0
org.springframework.boot.web.servlet.support.SpringBootServletInitializer
【讨论】:
【参考方案2】:这可能是您源代码中的导入问题 - 您的 Gradle 构建脚本使用 Spring Boot 1.3.6.RELEASE,其中 SpringBootServletInitializer 具有以下完全限定名称:
org.springframework.boot.context.web.SpringBootServletInitializer
但是,您的 Maven pom.xml 使用 Spring Boot 1.4.0.BUILD-SNAPSHOT,其中包名更改为:
org.springframework.boot.web.support.SpringBootServletInitializer
因此,如果您转到 SampleJettyJspApplication 并将导入更改为
import org.springframework.boot.context.web.SpringBootServletInitializer;
一切都应该没问题。
或者,您可以更改 Gradle 构建脚本以导入 1.4.0.BUILD-SNAPSHOT,但这需要添加 Spring 的快照存储库:
buildscript
repositories
maven.url "http://repo.spring.io/snapshot"
mavenCentral()
dependencies
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.BUILD-SNAPSHOT")
【讨论】:
谢谢伙计。 Spring在文档中提到了哪里? "org.springframework.boot.context.web 包中的所有类都已被弃用并重新定位。" github.com/spring-projects/spring-boot/wiki/…【参考方案3】:您应该使用与 maven 中相同的 spring boot 版本 1.4.0.BUILD-SNAPSHOT。 org.springframework.boot.web.support.SpringBootServletInitializer
是从 1.4.0 开始引入的,这就是 gradle 找不到它的原因。
【讨论】:
添加依赖失败classpath "org.springframework.boot:spring-boot-gradle-plugin:1.4.0.BUILD-SNAPSHOT"
gradle 无法重新配置
这是因为 1.4.0.BUILD-SNAPSHOT 仅位于 Spring 的快照存储库中,而不位于 Maven 中心,请查看我的代码示例答案。
按照@Miloš 的建议将 spring 存储库添加到 gradle。 Spring Boot Gradle 插件文档docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/html/…【参考方案4】:
org.springframework.boot.web.support.SpringBootServletInitializer
已弃用。
改用下面的:
org.springframework.boot.web.servlet.support.SpringBootServletInitializer
【讨论】:
以上是关于org.springframework.boot.web.support 不存在的主要内容,如果未能解决你的问题,请参考以下文章
插件 [id: 'org.springframework.boot', version: '2.0.2.RELEASE'] 未找到
Spring Boot 应用程序无法解析 org.springframework.boot 包
org.springframework.boot.web.support 不存在
将 org.springframework.boot 加载到项目中的 Gradle 插件
Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found
java.lang.ClassNotFoundException:org.springframework.boot.SpringApplication Maven