我是Spring / Maven的新手,并且遵循本教程: Serving Web Content with Spring MVC
每次我运行mvn spring-boot:run
时,都会收到此错误:
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run (default-cli) on project gs-serving-web-content: Could not exec java: Application finished with exit code: 1 ->
我尝试添加classpath,尝试运行mvn install clean spring-boot:run
,在类似的情况下做了很多其他人在stackoverflow上建议的事情,花费了8个多小时 - 没用。
这是我的主要课程Application.java
:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception{
SpringApplication.run(Application.class, args);
}
}
这是我的GreeetingController.java
课程:
package hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class GreetingController {
@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";
}
}
这是我的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>
<groupId>org.springframework</groupId>
<artifactId>gs-serving-web-content</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Exclusions to allow SpringBoot execute on HCP -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</exclusion>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
<!-- The main class to start by executing java -jar -->
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>hello.Application</mainClass>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
这是我的HTML模板:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
项目的结构是
src/main/java/hello/pom.xml
src/main/java/hello/Application.java
src/main/java/hello/GreetingController.java
src/main/resources/templates/greeting.html
答案 0 :(得分:2)
我进行了以下更改以使mvn clean spring-boot:run
有效:
pom.xml
移动到根目录,使目录层次结构为:目录层次结构:
.
├── pom.xml
└── src
└── main
├── java
│ └── hello
│ ├── Application.java
│ └── GreetingController.java
└── resources
└── templates
└── greeting.html
extensions
:注释掉部分:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Exclusions to allow SpringBoot execute on HCP -->
<!--<exclusions>-->
<!--<exclusion>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
<!--</exclusion>-->
<!--<exclusion>-->
<!--<groupId>org.apache.tomcat.embed</groupId>-->
<!--<artifactId>tomcat-embed-el</artifactId>-->
<!--</exclusion>-->
<!--<exclusion>-->
<!--<artifactId>logback-classic</artifactId>-->
<!--<groupId>ch.qos.logback</groupId>-->
<!--</exclusion>-->
<!--</exclusions>-->
</dependency>
您似乎打算排除这些依赖项。如果排除了嵌入式tomcat,mvn clean spring-boot:run
将成功退出,但我认为这是正确的行为,因为没有容器来部署应用程序。无论如何,您可以尝试并根据您的要求进行更改。
答案 1 :(得分:1)
有时该端口可能已经在使用中,请确保在运行应用程序之前先杀死所有Java进程。
答案 2 :(得分:0)
我和您有同样的错误,最后我发现它是由“ spring-boot-starter-parent”和“ spring-boot-autoconfigure”之间错误的协作版本引起的,并确保使用相同的版本。并检查其版本是否为最新版本。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
答案 3 :(得分:0)
就我而言,我只是删除“ devtools”依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
它有效!
答案 4 :(得分:0)
就我而言,我只是删除“ tomcat-embed-el”依赖项。
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</exclusion>
它有效.....