https://kotlinlang.org logo
Title
a

Alexander Suraphel

11/01/2021, 3:42 PM
Hi all, Ktor looks awesome and we’re planning to try it out with an API endpoint. We have an existing spring app which has a service class with methods that we want to call from Ktor. How should be go about it? RPC? If so what tools do you recommend?
h

Holger Steinhauer [Mod]

11/01/2021, 4:22 PM
Depends a bit on how your architecture looks like.
a

Alexander Suraphel

11/01/2021, 4:23 PM
It’s based on spring
h

Holger Steinhauer [Mod]

11/01/2021, 4:23 PM
The “cleanest” cut would be interfacing via REST or messaging. Then again, the use case might demand something else
Yeah, sure. But are we talking services or monolith? Is the application that uses Spring running in a single instance or clustered? What will the “class” do?
a

Alexander Suraphel

11/01/2021, 4:24 PM
Single instance
i just does some operation and return a result
h

Holger Steinhauer [Mod]

11/01/2021, 4:25 PM
You shouldn’t look at it as a “class” btw. If it provides a useful functionallity, make it available as a service
a

Alexander Suraphel

11/01/2021, 4:38 PM
It’s implemented as service class. I’m just wondering the best way to expose it to an app hosted on the same server
h

Holger Steinhauer [Mod]

11/01/2021, 4:39 PM
Ah, sorry. Spring’s
@Service
classes have nothing to do with a service that is available from the outside world. It is the “service layer”, where the business logic should be implemented
You could create a
@Controller
making it available as a REST service. This brings up the question how to authenticate and authorize.
Then, if you have access to the class itself, you could refactor the actual logic into a little library which your Spring and your Ktor app can then use
As I said, it highly depends on what you are actually trying to implement
“Making it available somehow when it runs on the same server” sounds a bit hacky to me
Also, in order to make things like this possible you need to make sure the apps run on the same JVM instance.
a

Alexander Suraphel

11/01/2021, 4:50 PM
I thought RPC was the standard way of doing such things but Controller seems doable too