java - 无法使用 Spring+Maven 退出代码 1 执行 java

标签 java spring maven spring-mvc

我是 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 ->

我尝试添加类路径,尝试运行 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

最佳答案

有时端口可能已被使用,请确保在运行应用程序之前终止所有 java 进程。

关于java - 无法使用 Spring+Maven 退出代码 1 执行 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43157515/

相关文章:

java - 从 Java 打开一个新的提示/终端窗口

java - Spring AOP - 切入点未触发

java - 在动态 Web 项目中使用 m2e 插件

maven - Gradle 可以将不同的 Artifact 上传到不同的 Maven 存储库吗?

java - Spring Hibernate 在测试中保存实体不持久

java - 在tomcat中运行应用程序时出错

java - 在第一个循环处继续,在第二个循环内

java - HQL 查询从 @ElementCollection 获取字段

java - 在指定位置将一张图像合并到另一张图像上并将其另存为新图像?

java - 在 Jetty 8.1.12 中将 dirAllowed 设置为 false