I tried out 100% Kotlin Frontend and Backend project demo using Kotlin Multiplattform → Fullstack Web Application. It is nice to see how common code is shared.
But how can I share common pojo between the mulitplattform project and in another non-kotlin project (a java only server)?
I have created a WebApp/Javascript only project (Multiplattform) and tried using it as dependency in the server using api(project(“:company.webapp”).
But when trying to create new Pojo class and assigning a kotlinx.datetime.LocalDateTIme, Intellj complains with this error:
gradle clean build, and Project rebuild do not complain -
- What is the recommended way sharing Pojos between Javaserver and Kotlin/Javascript?
- I tried using project company.webapp as dependency in company.server to use somehow the kotlin pojo classes in the server:
company.server:
GreetingController.java:
WsResultMessage2 wsResultMessage2 = new WsResultMessage2();
wsResultMessage2.setName("asds");
wsResultMessage2.setText("asa");
kotlinx.datetime.LocalDateTime sa = new kotlinx.datetime.LocalDateTime(2022, 12, 2, 12,23,3,2);
wsResultMessage2.setTime(sa); //Works in gradle clean build and Rebuild project, but in Editor error highlighting "Required type: kotlinx.datetime.LocalDateTime Provided: kotlinx.datetime.LocalDateTime"
pure Java17 company.server build file:
api project(':company.webapp')
company.webapp commonMain:
import kotlinx.datetime.LocalDateTime
open class WsResultMessage2 {
var time: LocalDateTime? = null
var name: String? = null
var text: String? = null
fun getSpecialTime(): LocalDateTime? {
return time;
}
}
company.webapp build.gradle.kts:
plugins {
kotlin("multiplatform") version "1.9.0-RC"
kotlin("plugin.serialization") version "1.9.0-RC"
}
tasks
.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask<*>>()
.configureEach {
compilerOptions.apiVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9)
compilerOptions.languageVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_1_9)
compilerOptions.suppressWarnings.set(false)
compilerOptions.verbose.set(true)
}
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "17"
}
withJava()
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}
js {
browser {
runTask {
outputFileName = "app.js"
}
webpackTask {
outputFileName = "app.js"
}
testTask {
enabled = true
useKarma {
useChromiumHeadless()
useFirefox()
}
}
}
binaries.executable()
}
sourceSets {
val commonMain by getting {
dependencies {
// api(project(":company.server.api"))
api("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val jsMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:2.3.1")
implementation("io.ktor:ktor-client-js:2.3.1")
implementation("io.ktor:ktor-client-websockets:2.3.1")
implementation("io.ktor:ktor-client-content-negotiation:2.3.1")
implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.1")
implementation("org.jetbrains.kotlinx:kotlinx-html:0.8.0")
}
}
val jsTest by getting {
dependencies {
implementation(kotlin("test-js"))
}
}
val jvmMain by getting {
dependencies {
// api("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
}
}
}
}