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

时间:2017-04-27 14:20:31

标签: java spring maven spring-boot

我使用spring initilizer创建了一个spring boot项目。我使用spring-boot-cli版本v1.5.3.RELEASE。

我只是试着跑:

mvn clean install spring-boot:run -e

但我的服务器不会继续运行,它会自动关闭。在端口8080上,我没有其他连接:

2017-04-27 17:19:28.306  INFO 1895 --- [           main] com.example.EmaApplication               : Starting EmaApplication on robucslvm03 with PID 1895 (/home/ge/SpringTest/ema/target/classes started by ge in /home/ge/SpringTest/ema)
2017-04-27 17:19:28.308  INFO 1895 --- [           main] com.example.EmaApplication               : No active profile set, falling back to default profiles: default
2017-04-27 17:19:28.336  INFO 1895 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6603c38b: startup date [Thu Apr 27 17:19:28 EEST 2017]; root of context hierarchy
2017-04-27 17:19:28.787  INFO 1895 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-04-27 17:19:28.795  INFO 1895 --- [           main] com.example.EmaApplication               : Started EmaApplication in 0.688 seconds (JVM running for 5.119)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.263 s
[INFO] Finished at: 2017-04-27T17:19:28+03:00
[INFO] Final Memory: 29M/407M
[INFO] ------------------------------------------------------------------------
2017-04-27 17:19:28.919  INFO 1895 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6603c38b: startup date [Thu Apr 27 17:19:28 EEST 2017]; root of context hierarchy
2017-04-27 17:19:28.921  INFO 1895 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

LATER EDIT:

 package com.example;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class DemoController {

        @RequestMapping(value="/echo")
            public String echo(@RequestParam(value="request", defaultValue="Hello!") String request) {
                return request;
            }
    }

仍然无法正常工作。 编辑:这是pom文件:

<?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>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.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-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>

2 个答案:

答案 0 :(得分:6)

这是因为您的应用根本不是网络应用。

将其转换为Web应用程序以使其保持运行的最佳方式是将Spring Boot Web依赖项添加到您的pom中,以便通过Spring Boot将Tomcat自动引入依赖项以运行应用程序一个网络应用程序。

<!-- https://mvnrepository.com/artifact/org.springframework.boot/‌​spring-boot-starter-‌​web --> 
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Edited to remove versioning and let Spring Boot Starter do it>
<version>1.5.3.RELEASE</version -->
</dependency>

答案 1 :(得分:1)

我在pom文件中添加了一个tomcat依赖项并且它有效。

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
</dependency>
相关问题