在春季启动2中如何在Java类中使用Kotlin类?

时间:2019-06-16 11:49:46

标签: java maven spring-boot kotlin

我使用以下配置从spring boot starter创建了一个Spring Boot项目:

  • 语言科特琳
  • 春季靴子 2.2.0 M3
  • 包装 war
  • Java: 12

现在,我添加2个简单的类,如下所示:

科特琳课:

class KotlinModel(
   var name: String = "",
   //var model: JavaModel
)

Java类:

class JavaModel {
   private String name = "";
   //private KotlinModel model;
}

取消注释//var model: JavaModel时,项目成功编译,但是取消注释//private KotlinModel model;时出现错误:

cannot find symbol
symbol: class KotlinModel
location: class fava.model.JavaModel

pom.xml如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.M3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
    <java.version>1.8</java.version>
    <kotlin.version>1.3.31</kotlin.version>
</properties>
<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-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-kotlin</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-reflect</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jdk8</artifactId>
    </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>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <configuration>
                <compilerPlugins>
                    <plugin>jpa</plugin>
                    <plugin>spring</plugin>
                </compilerPlugins>
                <args>
                    <arg>-Xjsr305=strict</arg>
                </args>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-noarg</artifactId>
                    <version>${kotlin.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-allopen</artifactId>
                    <version>${kotlin.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

问题出在哪里?我该如何解决?

0 个答案:

没有答案