Sure, here’s the translation: “Kotlin控制器类能否在Spring Boot中访问Java服务类?”

huangapple 未分类评论44阅读模式
英文:

Can Kotlin controller class access Java service class in spring boot

问题

在Spring Boot项目中,可以同时使用Kotlin和Java吗?我尝试过创建一个Kotlin的@Controller类并调用Java的@Service类,但似乎不起作用。

英文:

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

答案1

得分: 0

是的,它绝对会起作用

您只需要正确配置它

检查您的 build.gradle 源集和目标

按照下面的文件夹结构进行操作

   src
      main
         java
         kotlin
         resources
英文:

Yes, it will definitely work

You simply need to configure it properly

check your build.gradle source sets and targets

Follow the below folder structure

   src
      main
         java
         kotlin
         resources

答案2

得分: 0

我已经完成了以下步骤,成功地将 Kotlin 用于我的现有 Spring Boot 应用程序中。
我的 IDE 是 IntelliJ。

(1)

确保在 IDE 中安装了 Kotlin 插件。

  1. 选择 Tools -> Kotlin -> Configure Kotlin Plugin Updates。
  2. 在 Update channel 列表中,选择 Stable channel。
  3. 点击 Check again。最新的 Stable build 版本会显示出来。
  4. 点击 Install。应用 / 确定。

(2)

获取所需的依赖项。

  1. 打开 Spring Initializr(https://start.spring.io/)。
  2. 选择语言为 Kotlin。
  3. 保持其他设置不变。这里我们选择 Maven 项目。
  4. 点击 Explore。示例 pom.xml 将出现。
  5. 现在从示例 pom.xml 复制以下内容到您现有的 Spring Boot 项目的 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)

在您的项目中创建所需的目录。

  1. 在您的项目的 src/main 目录中,创建一个新目录并将其命名为 kotlin(此目录应与 src/main/java 处于同一级别)。
  2. 现在右键单击此目录 -> Mark Directory as -> Sources Root
  3. 同样,在您的项目的 src/test 目录中,创建一个新目录并将其命名为 kotlin(此目录应与 src/test/java 处于同一级别)。
  4. 现在右键单击此目录 -> Mark Directory as -> Test Sources Root

(4)

现在更新 / 重建项目。
如果出现任何 Maven / 构建错误,那么可以执行以下其中一项或全部操作:

  1. 右键单击 pom.xml -> Maven -> Download sources and documentation
  2. 右键单击 pom.xml -> Maven -> Generate sources and update folders
  3. 右键单击 pom.xml -> Maven -> Reload project

(5)

现在您可以在 src/main/kotlin 包下创建 Kotlin 类。
为此,

  1. 右键单击包 -> New -> Kotlin Class/File
  2. 给类取一个名称,双击 Class

注意:

如果新创建的 Kotlin 类没有 package 语句,则在您的 Java 类中将无法看到它。

要修复这个问题,

  1. 转到具有主方法的 Java 类。(这是一个被 @SpringBootApplication 注解标记的类)

  2. 复制此类的 package 语句。这很可能是 Java 类的第一个语句,应该类似于

     package com.xyz.myApplication;
    
  3. 将此语句粘贴到 Kotlin 类中,使其成为您的 Kotlin 类的第一个语句。

一旦所有步骤完成,您可以在 Java 类内部导入和实例化您的 Kotlin 类。
您也可以反过来,即从 Kotlin 类内部导入和实例化您的 Java 类。

英文:

I have done the following to successfully use Kotlin in my existing Spring Boot App.
My IDE is IntelliJ.

(1)

Ensure that Kotlin plugin is installed in the IDE.

  1. Select Tools -> Kotlin -> Configure Kotlin Plugin Updates.
  2. In the Update channel list, select the Stable channel.
  3. Click Check again. The latest Stable build version appears.
  4. Click Install. Apply / OK.

(2)

Get the required dependencies.

  1. Open Spring Initializr (https://start.spring.io/).
  2. Select language as Kotlin.
  3. Keep other things as usual. Here we are doing it for Maven Project.
  4. Click on Explore. Sample pom.xml will appear.
  5. Now copy the following from the sample pom.xml to the pom.xml of your existing Spring Boot Project.
…
&lt;properties&gt;
    …
    &lt;kotlin.version&gt;1.6.10&lt;/kotlin.version&gt;
    &lt;!--Edit the version to the latest stable version, if applicable--&gt;
&lt;/properties&gt;
…
&lt;dependencies&gt;
    …
    &lt;dependency&gt;
      &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
      &lt;artifactId&gt;kotlin-reflect&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
      &lt;artifactId&gt;kotlin-stdlib-jdk8&lt;/artifactId&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;
…
&lt;build&gt;
    &lt;sourceDirectory&gt;${project.basedir}/src/main/kotlin&lt;/sourceDirectory&gt;
    &lt;testSourceDirectory&gt;${project.basedir}/src/test/kotlin&lt;/testSourceDirectory&gt;
    …
&lt;/build&gt;
…
&lt;plugins&gt;
    …
    &lt;plugin&gt;
        &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
        &lt;artifactId&gt;kotlin-maven-plugin&lt;/artifactId&gt;
        &lt;configuration&gt;
          &lt;args&gt;
            &lt;arg&gt;-Xjsr305=strict&lt;/arg&gt;
          &lt;/args&gt;
          &lt;compilerPlugins&gt;
            &lt;plugin&gt;spring&lt;/plugin&gt;
          &lt;/compilerPlugins&gt;
        &lt;/configuration&gt;
        &lt;dependencies&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;org.jetbrains.kotlin&lt;/groupId&gt;
            &lt;artifactId&gt;kotlin-maven-allopen&lt;/artifactId&gt;
            &lt;version&gt;${kotlin.version}&lt;/version&gt;
          &lt;/dependency&gt;
        &lt;/dependencies&gt;
    &lt;/plugin&gt;
&lt;/plugins&gt;

(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)
  2. Now right click this directory -> Mark Directory as -> 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)
  4. Now right click this directory -> Mark Directory as -> Test Sources Root

(4)

Now update / rebuild the project.
If any Maven / build errors show up then do one / all of the following:

  1. Right click pom.xml -> Maven -> Download sources and documentation
  2. Right click pom.xml -> Maven -> Generate sources and update folders
  3. Right click pom.xml -> Maven -> Reload project

(5)

Now you can create a Kotlin class under the src/main/kotlin package.
For this,

  1. Right click the package -> New -> Kotlin Class/File
  2. Give the class some name and double click Class

Note:

If the newly created Kotlin class does not have a package statement then it will not be visible from your Java classes.

To fix this,

  1. Go to your Java class which has the main method. (This is the class which is annotated by @SpringBootApplication)

  2. Copy the package statement of this class. This should most likely be the first statement of the Java class and should be something like

    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.

Once everything is done, you can simply import and instantiate your Kotlin classes from inside your Java classes.
You can also do vice versa, that is, import and instantiate your Java classes from inside your Kotlin classes.

huangapple
  • 本文由 发表于 2020年4月7日 12:15:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/61072758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定