我是Spring Boot的新手,并根据一些视频教程开始使用。我创建了一个Maven项目,添加了工件id spring-boot-starter-parent的父依赖项,添加了spring-boot-starter-web和spring-boot-starter-test的依赖项。这是Application.java
@RestController
@EnableAutoConfiguration
public class Application {
@RequestMapping("/")
public String home() {
return "Hello World";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}备注:
src/test/java和src/test/resources包为空。这是一个Maven项目,我正在使用STS,执行"Maven ->更新项目...“并使用STS IDE多次清理/构建整个项目,我得到了以下错误。此外,我尝试在STS中创建一个新项目,但仍然收到以下错误:
当我将此作为Spring Boot App运行时,我收到此错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/util/Assert
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:263)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:247)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234)
at com.boot.Application.main(Application.java:18)
Caused by: java.lang.ClassNotFoundException: org.springframework.util.Assert
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more请帮我解决这个问题。
编辑:
这是我的pom.xml:
<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>
<groupId>com.deloitte.boot</groupId>
<artifactId>boot-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-test</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>这是我的项目目录结构:

发布于 2018-03-18 18:40:01
在学习和制作项目时,最好遵循软件原则。尝试以一种始终实现关注点分离的方式进行项目,这将使您的代码不仅易于理解,而且还可以进行调试和修复。
如下所示更改您的应用程序类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}现在创建另一个包控制器,并在其中创建用于测试的类
@RestController
//You can add this if you want or remove this class level mapping
@RequestMapping("/testApp")
public class TestController {
@RequestMapping("/")
public String home() {
return "Hello World";
}
}寻求进一步的帮助。请查看STS中Spring Boot App的this (official spring.io tutorial)简单示例。另一个是让Spring Boot App启动并运行的very simple and straightforward。
编辑:请同时在pom.xml中添加入门测试。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.10.RELEASE</version>
<scope>test</scope>
</dependency>发布于 2018-08-20 07:09:03
我已经修复了这样的问题,首先我删除了我的文件夹.m2,然后我运行mvn clean install到我的目录spring boot项目中。就这样!
发布于 2019-01-17 12:29:58
IntelliJ和一个从Spring Initializer创建的全新项目也有同样的问题。在尝试手动编译时看到此消息:
[ERROR] error reading /Users/mike/.m2/repository/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar; zip file is empty发现从maven central下载的文件已损坏:
ls -al /Users/mike/.m2/repository/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar
-rw-r--r-- 1 mike staff 0 Jan 16 20:55
/Users/mike/.m2/repository/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar删除整个目录并重新构建会强制它下载缺少的库的新副本:
./mvnw compile
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.pom (3.6 kB at 7.6 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/spring-core/5.1.4.RELEASE/spring-core-5.1.4.RELEASE.jar (1.3 MB at 3.6 MB/s)https://stackoverflow.com/questions/49346778
复制相似问题