java - 运行基本 Spring Boot 应用程序时出现问题

标签 java spring maven curl

我一直在研究介绍性的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 从单独的终端窗口 而不是预期的:

来自 Spring Boot 的问候!

我得到: curl: (7) 无法连接到本地主机端口 8080:连接被拒绝

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

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

谢谢

最佳答案

原因是 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/complete , https://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 添加到该依赖项中可以解决此问题。

关于java - 运行基本 Spring Boot 应用程序时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59803702/

相关文章:

java - Android 中使用 MySQL/JDBC 时未找到 Throwable.initCause

java - LDAP 最佳实践

java - libGDX Sprite 表 2d 动画

java - 事件处理程序,定义和访问参数(java Swing Netbeans)

java - 使用 Spring 3 在应用程序引擎中处理错误 (400)

java - Android 找不到 JSONValue 类

java - 为什么当我尝试将 Spring boot 应用程序推送到 heroku 时它返回 "Fatal error compiling: invalid target release: 11"

java - 是否有适合 Java 1.4 和 SE (Swing) 应用程序的优秀 DI 框架?

android - 离线隔离环境中的Android Studio

java - 无法将参数从命令行传递到 Maven 项目。我想传递可以运行测试的服务器 URL