Spring 启动应用程序未启动,即使使用 IDE(eclipse) 或命令提示符启动时 运行

Spring boot application not up and run when start even using IDE(eclipse) or command prompt

只需在 eclipse 中创建一个 spring 引导应用程序并尝试 运行 它,但它没有正确启动以处理请求。下面显示了控制台 尝试了在同一问题上提出的其他问题的所有解决方案。但没有用。

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.6.RELEASE)

2018-10-22 14:12:42.929  INFO 5752 --- [           main] com.nevro.TrainingApiApp                 : Starting TrainingApiApp on DESKTOP-AUANCVF with PID 5752 (E:\MyStuff\Tutorials\spring-boot-training\target\classes started by acer in E:\MyStuff\Tutorials\spring-boot-training)
2018-10-22 14:12:42.932  INFO 5752 --- [           main] com.nevro.TrainingApiApp                 : No active profile set, falling back to default profiles: default
2018-10-22 14:12:43.004  INFO 5752 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@19d37183: startup date [Mon Oct 22 14:12:43 IST 2018]; root of context hierarchy
2018-10-22 14:12:43.758  INFO 5752 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-10-22 14:12:43.770  INFO 5752 --- [           main] com.nevro.TrainingApiApp                 : Started TrainingApiApp in 1.137 seconds (JVM running for 1.725)
2018-10-22 14:12:43.783  INFO 5752 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@19d37183: startup date [Mon Oct 22 14:12:43 IST 2018]; root of context hierarchy
2018-10-22 14:12:43.785  INFO 5752 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

它说应用程序已启动,但是当尝试浏览它时,除了浏览器错误之外什么都没有。

tomcat (8080) 的默认端口未被占用。

这是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.nevro</groupId>
    <artifactId>spring-training</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-theory training</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <properties>
     <java.version>1.8</java.version>
    </properties>

</project>

这是 Main class 和 main 方法

package com.nevro;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TrainingApiApp {
    public static void main(String[] args) {
        SpringApplication.run(TrainingApiApp.class, args);
    }
}

也有一个控制器如下

package controller;

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

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String sayHi(){
        return "hi";
    }


}

任何原因以及如何解决这个问题。

您可能缺少控制器,请尝试添加一个。我在下面包含了一个非常基本的示例。

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

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }
}

The class is flagged as a @RestController, meaning it’s ready for use by Spring MVC to handle web requests. @RequestMapping maps / to the index() method. When invoked from a browser or using curl on the command line, the method returns pure text. That’s because @RestController combines @Controller and @ResponseBody, two annotations that results in web requests returning data rather than a view.

来源:Building an Application with Spring Boot

添加控制器后重新启动您的应用程序并访问 localhost:8080/,您现在应该会看到 Greetings from Spring Boot!

通过在 application.properties 中添加以下行尝试使用其他端口号:

server.port = 8088

如果仍然无效,请更改您安装的 JDK 版本。

对于spring启动2,你需要jdk9.

我认为问题出在您的控制器包上:package controller;

您的主要 class 在其他包中:package com.nevro; 所以当 Spring Boot 试图找到控制器时它找不到任何东西

Spring Boot 会自动检测com.nevro 和子包中的所有bean。但是,您的控制器位于 controller 包中,因此永远不会被检测到。将控制器移至 com.nevro.controller 包,这将使 Spring 启动检测控制器并帮助决定是否需要启动 Web 服务器。

也可能是其中一个依赖项没有正确下载。您可能想要使用 mvn dependency:purge-local-repository 清除本地存储库,然后重建应用程序以重新下载依赖项。