【问题标题】:Problem running basic Spring Boot application运行基本 Spring Boot 应用程序的问题
【发布时间】:2020-01-18 18:37:42
【问题描述】:

我一直在研究一个介绍性的Spring Boot tutorial

我已经跑到最远了

./mvnw spring-boot:run

来自项目目录。

生成输出:

Let's inspect the beans provided by Spring Boot:
application
beanNameHandlerMapping
defaultServletHandlerMapping
dispatcherServlet
embeddedServletContainerCustomizerBeanPostProcessor
handlerExceptionResolver
helloController
httpRequestHandlerAdapter
messageSource
mvcContentNegotiationManager
mvcConversionService
mvcValidator
org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$DispatcherServletConfiguration
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration
org.springframework.boot.context.embedded.properties.ServerProperties
org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
propertySourcesBinder
propertySourcesPlaceholderConfigurer
requestMappingHandlerAdapter
requestMappingHandlerMapping
resourceHandlerMapping
simpleControllerHandlerAdapter
tomcatEmbeddedServletContainerFactory
viewControllerHandlerMapping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.530 s
[INFO] Finished at: 2020-01-18T14:29:53-04:00
[INFO] ------------------------------------------------------------------------

但是当我运行$ curl localhost:8080 从单独的终端窗口 而不是预期的:

Greetings from Spring Boot!

我得到: curl: (7) Failed to connect to localhost port 8080: Connection refused

这是我的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


    </dependencies>



    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

任何帮助或见解将不胜感激。

谢谢

【问题讨论】:

  • 您是否按 Ctrl-C 来停止 ./mvnw spring-boot:run 命令?因为如果你这样做了,你就停止了服务器。
  • @JBNizet 我没有按 Ctrl-C,但是,该命令自行完成并返回到输入行。我现在意识到这表明服务器没有运行。我将不得不找出原因。感谢您提请我注意。
  • 你应该有比控制台更多的输出。可能包含解释问题所在的错误消息。
  • @JBNizet 我的 pom.xml 文件中似乎存在问题......我直接从教程中复制了一个,而不是使用 Spring 初始化程序中的那个,我承认我已经修改过.. . 它现在可以工作了。
  • 你必须启动你的应用程序。运行 ./mvnw spring-boot: 再次运行。

标签: java spring maven curl


【解决方案1】:

直接从教程中复制 pom.xml 文件后,我注意到可能是一个重要的区别:

新的(工作):

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

旧:

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

注意 spring-boot-start-web vs spring-boot-starter

基于来自this stackoverflow post 的信息 spring-boot-starter-web 包括:

  • spring-boot-starter
  • 杰克逊
  • 弹簧芯
  • spring-mvc
  • spring-boot-starter-tomcat

最初我使用的是 spring-boot-starter 而不是 spring-boot-starter-web 并手动添加了 spring-mvc。很可能还需要其他一些依赖项。

【讨论】:

    【解决方案2】:

    原因是 Spring 初始化器的 pom.xml 有错误的依赖关系。

    当你在这里使用 Spring 初始化器https://start.spring.io/ 时,pom.xml 中的依赖项之一看起来像这样

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
    

    但是在https://github.com/spring-guides/gs-spring-boot/blob/master/completehttps://github.com/spring-guides/gs-spring-boot/tree/master/initial 和教程页面https://spring.io/guides/gs/spring-boot/ 上,pom.xml 中的依赖项应该是这样的

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    

    将 -web 添加到该依赖项将解决此问题。

    【讨论】:

      【解决方案3】:

      我尝试在命令行中按照以下步骤操作并成功:

      git clone https://github.com/spring-guides/gs-spring-boot.git
      

      克隆完成后,将创建一个新目录 gs-spring-boot,然后将目录更改为 gs-spring-boot/initial

      cd gs-spring-boot/initial
      

      然后运行以下命令

      ./mvnw spring-boot:run
      

      如果您已经安装了 maven 并且在您的路径中可用,则可以使用以下命令

      mvn spring-boot:run
      

      然后spring应用启动完成后spring应用开始运行,不用按Ctrl+c和终止应用切换到postman并指向地址:

      localhost:8080
      

      然后你会看到如下预期的结果

      【讨论】:

        猜你喜欢
        • 2018-05-18
        • 2019-08-27
        • 2016-05-20
        • 2021-01-07
        • 1970-01-01
        • 1970-01-01
        • 2019-07-25
        • 2020-10-19
        • 2020-07-13
        相关资源
        最近更新 更多