java - Spring Boot 不起作用?

标签 java spring maven spring-mvc spring-boot

我已经使用命令生成了一个标准的 Spring Boot 示例

spring init

结果是一个普通的 Java 项目,其 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>org.test</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.2.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>demo.DemoApplication</start-class>
        <java.version>1.7</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>

当我击中

spring run src/main/java/demo/DemoApplication.java

它会显示一条错误消息,例如

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'propertySourceBootstrapConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.propertySourceLocators; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configServicePropertySource' defined in class org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration$PropertySourceLocatorConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.config.client.ConfigServicePropertySourceLocator]: Factory method 'configServicePropertySource' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/web/client/HttpServerErrorException

我无法解释。 我做错了什么?

最佳答案

这个问题的标签之一是 spring-mvc,所以我假设您想使用 Web Spring 功能。 要启用 Spring Web 功能,您需要将此 Spring Boot 依赖项添加到 POM 中:

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

我之前没有使用过Spring CLI,因此我不知道为什么它没有生成带有Web依赖项的项目。但我敢打赌,您需要以某种方式明确指定要生成 Web 项目。

因此,您可能生成了没有 Web 依赖项的普通 Spring Boot 项目,并在 application.properties 中添加了一些与 Web 相关的配置。因此 Spring Boot 自动配置器会尝试在您的类路径上查找 Web 依赖项。

关于java - Spring Boot 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30153393/

相关文章:

java - JPA 规范搜索字符串列表包含

java - Android 拍照并保存到变量

spring - 如何在异常时设置文字字符串 flowScope 值?

java - 执行 org.apache.maven.plugins :maven-war-plugin:2. 1.1:war 时缺少必需的类

java - 在行中找不到可执行代码

java.util.logging.SimpleFormatter.format 毫秒

java - Java 如何接受我的自签名证书而无需将其添加到 Java 信任库

java - Spring boot 中 jQuery AJAX 请求出现 404

java - @PrePersist 正在被调用并修改实体,但没有写入数据库

java - WSDL 生成可重复构建