简体   繁体   中英

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 have tried by making a Kotlin @controller class and calling java @service class but it isn't working

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

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.
…
<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)

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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