简体   繁体   English

Can Kotlin controller class access Java service class in spring boot

[英]Can Kotlin controller class access Java service class in spring boot

I a working on a project in spring boot.. is it possible to use in kotlin and java both in spring-boot project. I a working on a project in spring boot.. is it possible to use in kotlin and java both in spring-boot project. I have tried by making a Kotlin @controller class and calling java @service class but it isn't working我尝试制作 Kotlin @controller class并调用 java @service ZA2F2ED4F8EBC2CBB4C21A29 ,但它不是工作

Yes, it will definitely work是的,它肯定会起作用

You simply need to configure it properly您只需要正确配置它

check your build.gradle source sets and targets检查您的 build.gradle 源集和目标

Follow the below folder structure遵循以下文件夹结构

   src
      main
         java
         kotlin
         resources

I have done the following to successfully use Kotlin in my existing Spring Boot App.我已完成以下操作以在我现有的 Spring 启动应用程序中成功使用 Kotlin。 My IDE is IntelliJ.我的 IDE 是 IntelliJ。

(1) (1)

Ensure that Kotlin plugin is installed in the IDE.确保 Kotlin 插件安装在 IDE 中。

  1. Select Tools -> Kotlin -> Configure Kotlin Plugin Updates. Select 工具 -> Kotlin -> 配置 Kotlin 插件更新。
  2. In the Update channel list, select the Stable channel.在更新频道列表中,select 是稳定频道。
  3. Click Check again.再次单击检查。 The latest Stable build version appears.出现最新的稳定版本。
  4. Click Install.单击安装。 Apply / OK.应用/确定。

(2) (2)

Get the required dependencies.获取所需的依赖项。

  1. Open Spring Initializr ( https://start.spring.io/ ).打开 Spring Initializr ( https://start.spring.io/ )。
  2. Select language as Kotlin. Select 语言为 Kotlin。
  3. Keep other things as usual.其他事情照常进行。 Here we are doing it for Maven Project.在这里,我们正在为 Maven 项目做这件事。
  4. Click on Explore.点击探索。 Sample pom.xml will appear.将出现示例 pom.xml。
  5. Now copy the following from the sample pom.xml to the pom.xml of your existing Spring Boot Project.现在将示例 pom.xml 中的以下内容复制到现有 Spring 引导项目的 pom.xml 中。
…
<properties>
    …
    <kotlin.version>1.6.10</kotlin.version>
    <!--Edit the version to the latest stable version, if applicable-->
</properties>
…
<dependencies>
    …
    <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>
    …
</build>
…
<plugins>
    …
    <plugin>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-maven-plugin</artifactId>
        <configuration>
          <args>
            <arg>-Xjsr305=strict</arg>
          </args>
          <compilerPlugins>
            <plugin>spring</plugin>
          </compilerPlugins>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlin.version}</version>
          </dependency>
        </dependencies>
    </plugin>
</plugins>

(3) (3)

Create the required directories in your project.在您的项目中创建所需的目录。

  1. In the src/main directory of your project, create a new directory and name it as kotlin (this directory should be at the same level as src/main/java)在你的项目的src/main目录下,新建一个目录,命名为kotlin(这个目录应该和src/main/java同级)
  2. Now right click this directory -> Mark Directory as -> Sources Root现在右键单击此目录 -> 将目录标记为 -> Sources Root
  3. Also, in the src/test directory of your project, create a new directory and name it as kotlin (this directory should be at the same level as src/test/java)另外,在你项目的 src/test 目录下,新建一个目录,命名为 kotlin(这个目录应该和 src/test/java 同级)
  4. Now right click this directory -> Mark Directory as -> Test Sources Root现在右键单击此目录 -> 将目录标记为 -> 测试源根目录

(4) (4)

Now update / rebuild the project.现在更新/重建项目。 If any Maven / build errors show up then do one / all of the following:如果出现任何 Maven / 构建错误,请执行以下一项/所有操作:

  1. Right click pom.xml -> Maven -> Download sources and documentation右键 pom.xml -> Maven -> 下载源代码和文档
  2. Right click pom.xml -> Maven -> Generate sources and update folders右键 pom.xml -> Maven -> 生成源和更新文件夹
  3. Right click pom.xml -> Maven -> Reload project右键 pom.xml -> Maven -> 重新加载项目

(5) (5)

Now you can create a Kotlin class under the src/main/kotlin package.现在您可以在 src/main/kotlin package 下创建 Kotlin class。 For this,为了这,

  1. Right click the package -> New -> Kotlin Class/File右键单击 package -> 新建 -> Kotlin 类/文件
  2. Give the class some name and double click Class为 class 命名并双击 Class

Note:笔记:

If the Kotlin class does not have a package statement then it will not be visible from your Java classes.如果 Kotlin class 没有 package 语句,则它不会从您的 ZD52387880E1EA213817A 类中看到。

To fix this,为了解决这个问题,

  1. Go to your Java class which has the main method. Go 到您的 Java class 具有主要方法。 (This is the class which is annotated by @SpringBootApplication) (这是由@SpringBootApplication 注释的class)

  2. Copy the package statement of this class.复制此 class 的 package 语句。 This should most likely be the first statement of the Java class and should be something like这很可能是 Java class 的第一条语句,应该类似于

    package com.xyz.myApplication;
  3. Paste this statement in the Kotlin class so that this becomes the first statement of your Kotlin class as well.将此语句粘贴到 Kotlin class 中,使其成为 Kotlin ZA2F2ED4F8EBC0CBB4C21A29D 的第一条语句。

Once everything is done, you can simply import and instantiate your Kotlin classes from inside your Java classes.一切完成后,您可以从 Java 类中简单地导入和实例化 Kotlin 类。 You can also do vice versa, that is, import and instantiate your Java classes from inside your Kotlin classes.您也可以反之亦然,即从 Kotlin 类中导入并实例化您的 Java 类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM