Kotlin、Spring-boot 和 Maven:未解决的参考:SpringBootApplication

     2023-02-27     151

关键词:

【中文标题】Kotlin、Spring-boot 和 Maven:未解决的参考:SpringBootApplication【英文标题】:Kotlin, Spring-boot and Maven: Unresolved reference: SpringBootApplication 【发布时间】:2020-03-12 13:30:30 【问题描述】:

我想使用 Spring Boot 编写一个 Kotlin 应用程序。 我写了以下 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId> com.matan</groupId>
<version>0.0.1</version>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-5-mvc</artifactId>
<name>spring-5-mvc</name>
<description>spring 5 MVC sample project about new features</description>
<packaging>jar</packaging>

<properties>
    <jayway-rest-assured.version>2.9.0</jayway-rest-assured.version>
    <jackson.version>2.9.9</jackson.version> <!-- Same as spring-boot-dependencies:2.1.7.RELEASE -->
    <kotlin.version>1.3.50</kotlin.version> <!-- Same as spring-boot-dependencies:2.1.7.RELEASE -->
    <start-class>com.matan.Main</start-class>
    <httpclient.version>4.5.8</httpclient.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- utils -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
        <!--kotlin deps -->
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jre8</artifactId>
            <version>$kotlin.version</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
            <version>$kotlin.version</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-kotlin</artifactId>
            <version>$jackson.version</version>
        </dependency>
        <!-- runtime and test scoped -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>$jayway-rest-assured.version</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jdk8</artifactId>
        <version>$kotlin.version</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-test</artifactId>
        <version>$kotlin.version</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <sourceDirectory>src/main/kotlin</sourceDirectory>
    <testSourceDirectory>src/test/kotlin</testSourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.2.1.RELEASE</version>
        </plugin>
        <plugin>
            <artifactId>kotlin-maven-plugin</artifactId>
            <groupId>org.jetbrains.kotlin</groupId>
            <version>$kotlin.version</version>
            <configuration>
                <compilerPlugins>
                    <plugin>spring</plugin>
                </compilerPlugins>
                <jvmTarget>1.8</jvmTarget>
            </configuration>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-allopen</artifactId>
                    <version>$kotlin.version</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

我写了以下主类:

package com.matan;

@SpringBootApplication
class Main

然后我得到了错误:

kotlin:未解决的参考:SpringBootApplication

你知道我做错了什么吗?

【问题讨论】:

您的 pom 定义的唯一依赖项是 kotlin-stdlib-jdk8 和 kotlin-test。使用start.spring.io 生成正确的 pom.xml。 import org.springframework.boot.autoconfigure.SpringBootApplication 【参考方案1】:

这是迄今为止我发现的最“轻量级”的 pom.xml,它可以让 spring boot 与 kotlin 一起工作:

<?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">
    <groupId> com.matan</groupId>
    <version>0.0.1</version>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>spring-5-mvc</artifactId>
    <name>spring-5-mvc</name>
    <description>spring 5 MVC sample project about new features</description>
    <packaging>jar</packaging>

    <properties>
        <jayway-rest-assured.version>2.9.0</jayway-rest-assured.version>
        <jackson.version>2.9.9</jackson.version> <!-- Same as spring-boot-dependencies:2.1.7.RELEASE -->
        <java.version>1.8</java.version>
        <kotlin.version>1.3.50</kotlin.version> <!-- Same as spring-boot-dependencies:2.1.7.RELEASE -->
        <start-class>com.matan.Main</start-class>
        <httpclient.version>4.5.8</httpclient.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- See https://github.com/spring-projects/spring-boot/issues/18593 and https://github.com/h2database/h2database/issues/1841 -->
        <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>
                <exclusion>
                    <groupId>org.mockito</groupId>
                    <artifactId>mockito-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>$project.basedir/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>$project.basedir/src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>kotlin-maven-plugin</artifactId>
                <groupId>org.jetbrains.kotlin</groupId>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                        <plugin>jpa</plugin>
                        <plugin>all-open</plugin>
                    </compilerPlugins>
                    <pluginOptions>
                        <option>all-open:annotation=javax.persistence.Entity</option>
                        <option>all-open:annotation=javax.persistence.Embeddable</option>
                        <option>all-open:annotation=javax.persistence.MappedSuperclass</option>
                    </pluginOptions>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>$kotlin.version</version>
                    </dependency>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-noarg</artifactId>
                        <version>$kotlin.version</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>kapt</id>
                        <goals>
                            <goal>kapt</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>src/main/kotlin</sourceDir>
                            </sourceDirs>
                            <annotationProcessorPaths>
                                <annotationProcessorPath>
                                    <groupId>org.springframework.boot</groupId>
                                    <artifactId>spring-boot-configuration-processor</artifactId>
                                    <version>$project.parent.version</version>
                                </annotationProcessorPath>
                            </annotationProcessorPaths>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

【讨论】:

如何启动 2.4.0 和 kotlin 1.4.20

】如何启动2.4.0和kotlin1.4.20【英文标题】:Howtospring-boot2.4.0andkotlin1.4.20【发布时间】:2021-03-1800:09:05【问题描述】:我正在尝试构建一个spring-boot微服务。该构建使用的是spring-boot的releasee2.4.0。我正在尝试使用新版本1.4.20的kotlin... 查看详情

缺少 JSON 密钥(在带有 kotlin 的 Spring-boot 上使用 @JsonComponent)

】缺少JSON密钥(在带有kotlin的Spring-boot上使用@JsonComponent)【英文标题】:JSONkeyismissing(using@JsonComponentonSpring-bootwithkotlin)【发布时间】:2021-11-2512:41:43【问题描述】:感谢阅读这个问题。这个问题让我很困惑。我创建了响应JSON数... 查看详情

在 spring-boot 中使用 kotlin 挂起函数创建名称为“requestMappingHandlerMapping”的 bean 时出错

】在spring-boot中使用kotlin挂起函数创建名称为“requestMappingHandlerMapping”的bean时出错【英文标题】:Errorcreatingbeanwithname\'requestMappingHandlerMapping\'withkotlinsuspendfunctioninspring-boot【发布时间】:2020-08-0610:53:36【问题描述】:spring-boot版... 查看详情

JPA 自动完成功能在 Intellij spring-boot kotlin 项目中不起作用

】JPA自动完成功能在Intellijspring-bootkotlin项目中不起作用【英文标题】:JPAautocompletionisnotworkinginIntellijispring-bootkotlinproject【发布时间】:2019-03-2709:51:42【问题描述】:创建了一个简单的JPAspringboot应用程序,如附加的屏幕截图,我... 查看详情

带有用户和角色的 Kotlin 中的 Spring Boot 安全性

...】:我必须处理一个老实习生开始的应用程序。后端是用spring-boot和Kotlin制作的,我对这两者都很陌生。支持的是RESTfulAPI,我需要实现身份验证和授权,以限制特定用户对CRUD方法的访问。基本上我需要创 查看详情

使用 spring-boot 的 assemble 创建的 jar 的 Gradle 集名称

】使用spring-boot的assemble创建的jar的Gradle集名称【英文标题】:Gradlesetnameforjarcreatedwithspring-boot\'sassemble【发布时间】:2021-06-1113:44:19【问题描述】:我想用gradle(kotlin-dsl)创建一个可执行的jar,我想给它一个自定义名称。对于可执... 查看详情

Kotlin - 数据类实体抛出 ***Error

...8-08-0206:16:38【问题描述】:我尝试将kotlin(版本1.2.21)与spring-boot(1.5.9.RELEASE)结合起来。我在使用带有@Entity注释的数据类时遇到了问题。我有问题的实体如下所示:@Entity@Table(name="APP_USER")da 查看详情

如何在没有 spring-boot 的情况下在 spring-webflux 中加载配置?

】如何在没有spring-boot的情况下在spring-webflux中加载配置?【英文标题】:howtoloadconfiginspring-webfluxwithoutspring-boot?【发布时间】:2018-03-3118:42:00【问题描述】:我只是用springwebflux5.0.0和Kotlin做了一些实验,我在从application.yml加载配... 查看详情

如何解决 CockroachDB 和 Spring-Boot 的密码问题

】如何解决CockroachDB和Spring-Boot的密码问题【英文标题】:HowtofixpasswordproblemwithCockroachDBandSpring-Boot【发布时间】:2020-01-0608:33:03【问题描述】:我对spring-boot和cockroachDB有疑问。在我的本地机器上运行cockraochDB和spring-boot。一旦sprin... 查看详情

QueryDSL、spring-boot 和 Gradle

】QueryDSL、spring-boot和Gradle【英文标题】:QueryDSL,spring-boot&Gradle【发布时间】:2014-05-1112:06:07【问题描述】:我希望通过gradle将querydsl带入我的spring-boot项目。尽管在网上找到了几个例子,但由于依赖关系的问题(我认为),它... 查看详情

Spring-Boot 和 Spring-Security 配置

】Spring-Boot和Spring-Security配置【英文标题】:Spring-Boot&Spring-SecurityConfiguration【发布时间】:2014-07-2521:27:41【问题描述】:我正在尝试在我的Spring-Boot项目中使用SpringSecurity。我的项目结构是:/project-/src/main--/java--/resources---/stati... 查看详情

spring-boot 和 shedlock - 如何追踪依赖冲突?

】spring-boot和shedlock-如何追踪依赖冲突?【英文标题】:spring-bootandshedlock-howtotrackdowndependencyconflicts?【发布时间】:2020-01-1302:31:06【问题描述】:我需要在使用shedlock的微服务项目中更新spring-boot的次要版本。如果spring-boot是2.0.x... 查看详情

springboot学习笔记

...方便快速搭建应用 springboot官网:http://projects.spring.io/spring-boot/mave 查看详情

使用 spring-boot 和 Weblogic 公开 SOAP Webservice

】使用spring-boot和Weblogic公开SOAPWebservice【英文标题】:ExposingSOAPWebservicewithspring-bootandWeblogic【发布时间】:2015-03-1713:24:15【问题描述】:我正在编写一个我想在Weblogic12C中部署的spring-boot应用程序。该应用程序公开了一个SOAPWeb服... 查看详情

Spring-boot 和 Keycloak 集成

】Spring-boot和Keycloak集成【英文标题】:Spring-bootandKeycloakIntegration【发布时间】:2020-01-1107:00:35【问题描述】:我正在使用springboot2.1.7Release和keycloak-spring-boot-2-starter:4.0.0.Final版本。这个keycloak-spring-boot-2-starter会自动下载4.0.0对应... 查看详情

spring-boot:repackage 和 mvn package 的区别

】spring-boot:repackage和mvnpackage的区别【英文标题】:Differencebetweenspring-boot:repackageandmvnpackage【发布时间】:2017-08-1218:53:27【问题描述】:在阅读了Spring文档和网络上的其他一些文章后,我仍然很困惑SpringBootMaven插件的spring-boot:repac... 查看详情

在 spring-boot 中如何分离前端和后端机器?

】在spring-boot中如何分离前端和后端机器?【英文标题】:HowseperateFront-End&Back-Endmachineinspring-boot?【发布时间】:2015-10-1017:27:37【问题描述】:我想将后端和前端(HTML页面)机器分开。后端将由Spring-Boot开发。如何将控制器中... 查看详情

thymeleaf 和 spring-boot 丢失的复杂对象

】thymeleaf和spring-boot丢失的复杂对象【英文标题】:Complexobjectlostinfromwiththymeleafandspring-boot【发布时间】:2017-03-1207:39:14【问题描述】:我有一个内部有嵌套复杂对象的对象:publicclassMonitoringSystem@Id@GeneratedValue(strategy=GenerationType.A... 查看详情