Spring Boot应用程序在启动时终止

Uma*_*mar 7 java spring spring-boot

我正在尝试一个简单的弹簧启动应用程序,它总是自动关闭

 :: Spring Boot ::        (v1.4.1.RELEASE)
2016-10-23 13:05:21.681  INFO 16532 --- [           main] com.example.RestBootApplication          : No active profile set, falling back to default profiles: default
2016-10-23 13:05:21.766  INFO 16532 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6e20b53a: startup date [Sun Oct 23 13:05:21 EDT 2016]; root of context hierarchy
2016-10-23 13:05:23.682  INFO 16532 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-10-23 13:05:23.704  INFO 16532 --- [           main] com.example.RestBootApplication          : Started RestBootApplication in 2.632 seconds (JVM running for 5.168)
2016-10-23 13:05:23.705  INFO 16532 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6e20b53a: startup date [Sun Oct 23 13:05:21 EDT 2016]; root of context hierarchy
2016-10-23 13:05:23.708  INFO 16532 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
Run Code Online (Sandbox Code Playgroud)

的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>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>rest-boot</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
            </plugin>
        </plugins>
    </build>


</project>
Run Code Online (Sandbox Code Playgroud)

主类

@SpringBootApplication
public class RestBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(RestBootApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

调节器

@Controller
public class HelloController {

    @RequestMapping("/hello")
    String helloWorld(){
        return "helloWorld";
    }

}
Run Code Online (Sandbox Code Playgroud)

试图在spring工具套件中运行.它总是在开始后停止.在查看了一些stackoverflow问题之后,我甚至添加了"spring-boot-starter-web",但仍然面临着这个问题.

请问有人可以指出这个问题.

Dar*_*man 15

这发生在我身上,结果证明是一个损坏的maven apache存储库.

为了解决这个问题,我删除了apache repo - 在我的Mac上,它位于/Users/myname/.m2/repository/org/apache.

在PC上它应该是c:\ users\myname.m2\repository\org\apache.

然后我运行了Maven - Update Project,然后我运行了我的课程并修复了它.

  • 我遇到了这个问题,并且有很多关于这个确切问题的stackoverflow问题,之前没有人指出过这个问题.我想这就是为什么我们不喜欢有关同一事情的多个问题. (3认同)

jde*_*dev 10

在我的情况下,只需在pom文件中添加以下内容

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

  • `&lt;dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt; &lt;/dependency&gt; &lt;/dependency&gt; ` spring-boot-starter-web dependency 是使用 Spring MVC 构建 Web 应用程序(包括 RESTful 应用程序)的入门工具。它通过包含 spring-boot-starter-tomcat 使用 Tomcat Web 服务器作为默认的嵌入式容器,但您可以使用 spring-boot-starter-jetty 或 spring-boot-starter-undertow 来代替,它们是使用 Jetty 和 Undertow 作为启动器嵌入式 servlet 容器。 (2认同)

Jar*_*dCS 8

检查您获得的退出代码。

如果您收到“进程已完成,退出代码为 1”,则表示正在引发异常。因此,您可以在语句周围放置一个 try catch 块SpringApplication.run()并打印出堆栈跟踪。

public static void main(String[] args) {
    try {
        SpringApplication.run(MyApplication.class, args);
    } catch (Exception e) {
        e.printStackTrace(); 
    }
}
Run Code Online (Sandbox Code Playgroud)

示例来自:https ://stackoverflow.com/a/59017774/9531109


geo*_*van 1

我的建议是删除这种依赖

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

您还可以尝试添加此 tomcat 依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>1.4.1.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

  • `spring-boot-starter-web` 包含 `spring-boot-starter`,因此虽然没有必要单独声明它,但它不会造成损害,也不会导致问题。 (2认同)