简体   繁体   English

是否可以使用 Ktor 和/或 Kotlin 多平台在 Rest API 和 Android 应用程序之间“共享”暴露的数据?

[英]Is it possible to use Ktor and / or Kotlin multiplatform to "share" the exposed data between a Rest API and an Android Application?

The main idea is to have a sort of "serialization / deserialization module" that could be shared between a backend (using Ktor) and an Android application.主要思想是拥有一种可以在后端(使用 Ktor)和 Android 应用程序之间共享的“序列化/反序列化模块”。

I don't know how "far" this can go, but I would imagine that the routes are "exposed" for the frontend app to consume (like Retrofit does with its interface with annotations) and the (de-)serialization logic is shared too.我不知道 go 能“走多远”,但我想路由是“公开”供前端应用程序使用的(就像 Retrofit 对其带注释的接口所做的那样)并且(反)序列化逻辑是共享的也。

So basicaly, is it possible to "share" not only the "POJO / POKO" representations of API responses, but also the possible endpoints with the expected parameters, between a server (I guess using Ktor) and an Android application (Java / Kotlin)?所以基本上,是否可以在服务器(我想使用 Ktor)和 Android 应用程序(Java / Kotlin)之间“共享”不仅是 API 响应的“POJO / POKO”表示,还可以“共享”具有预期参数的可能端点)?

Yes. 是。

You can structure your code with a multi project gradle build like this: 您可以使用如下所示的多项目gradle构建来构建代码:

  • API module : Put your DTOs, searialization/deserialization and routes as constants here. API模块 :将您的DTO,searialization / deserialization和路由作为常量放在这里。 Define an http client to your api here as well (Ktor has an http client as well). 在这里也为你的api定义一个http客户端(Ktor也有一个http客户端)。 OpenAPI is also something you might look at for defining your http api and then just generating the client. OpenAPI也可以用于定义http api,然后生成客户端。
  • Server module : Depends on the API module to implement the routes with the DTOs defined. 服务器模块 :取决于API模块以实现定义了DTO的路由。 And if you defined a client in API, test that your server implementation satisfies it. 如果您在API中定义了一个客户端,请测试您的服务器实现是否满足它。
  • Android module : Depend on the API module. Android模块 :取决于API模块。 Use the client, and mock it for testing. 使用客户端,并模拟它进行测试。

This is a little bit like the Dependency Inversion Principal from SOLID. 这有点像SOLID的Dependency Inversion Principal You define the interface of the data exchange in a seperate module(API module). 您可以在单独的模块(API模块)中定义数据交换的接口。 And then, both the implementation (Server Module) and the higher level code (Android Module) depend on that for its use and implementation. 然后,实现(服务器模块)和更高级别的代码(Android模块)依赖于它的使用和实现。

I had tried to run something like this, it didn't work in my way我试过运行这样的东西,但它没有按我的方式工作

Main answer: Currently no.主要回答:目前没有。

It's impossible to share data between Ktor and Android in Kotlin Multiplatform paradigm.在 Kotlin Multiplatform 范式中,Ktor 和 Android 之间不可能共享数据。 Let's figure it out:让我们弄清楚:

  • Your sharing library will be multiplatform.您的共享库将是多平台的。 It builds in special platforms pieces to support all platforms它内置特殊平台部件以支持所有平台
  • Android is multiplatform and can obtain your library. Android 是多平台的,可以获取你的库。 When you will add your library Android KMM project will recognize the part that it needs.当您添加库时 Android KMM 项目将识别它需要的部分。
  • Ktor (as a server) is using JVM or Kotlin/Native. Ktor(作为服务器)使用 JVM 或 Kotlin/Native。 It means that you can't just provide your KMM library and put it in Ktor.这意味着你不能只提供你的 KMM 库并将它放在 Ktor 中。 Your library structure is configured for another KMM project and can't be obtained by Ktor, it doesn't know how to handle it.你的库结构是为另一个 KMM 项目配置的,Ktor 无法获取它,它不知道如何处理。

Multiplatform and Multiplatform are friends.多平台和多平台是朋友。 So who is not - eliminated那么谁不-淘汰

Thereotically answer: Yes.理论上回答:是的。

Since multiplatform library builds into parts one of them should be.jar and you can share it for Android project and Ktor project.由于多平台库构建成部分,其中之一应该是 .jar,您可以将其共享给 Android 项目和 Ktor 项目。 I don't know where you can find them, but i think it's possible.我不知道你在哪里可以找到它们,但我认为这是可能的。

In my case it's not appropriate because it's not cross-platform way.就我而言,这是不合适的,因为它不是跨平台的方式。 You can't have iOS, macOS in this scenario.在这种情况下,您不能拥有 iOS,macOS。 And you have to build and store your.jar separately.你必须单独构建和存储 your.jar。

Example of KMM library KMM 库示例

There are some facts about build artifacts 有一些关于构建工件的事实

暂无
暂无

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

相关问题 是否可以在多平台 Z1B4419ABC43A758B418ZA0CA50 应用程序中使用用 Kotlin 编写的 Android 和 iOS 的业务代码? - Is it possible to use business code for Android and iOS written in Kotlin in a multiplatform Xamarin application? Kotlin Multiplatform - iOS + Android 之间的策略共享 - Kotlin Multiplatform - Strategy sharing between iOS + Android Kotlin Multiplatform Android模块只能访问Android API 1 - Kotlin Multiplatform Android module only has access to Android API 1 在 Android 和 iOS 之间可以通过 Kotlin Multiplatform 共享什么? - What can be shared through Kotlin Multiplatform between Android and iOS? 将 Ktor 添加到 Kotlin Multiplatform Mobile 导致未解决的参考:HttpClient - Adding Ktor To Kotlin Multiplatform Mobile results in Unresolved reference: HttpClient 我如何使用 Ktor Client For Kotlin-multiplatform 执行这个 cURL? - how can i excute this cURL with Ktor Client For Kotlin-multiplatform? 在 Kotlin 多平台项目中使用 Ktor HttpClient 将文件作为二进制文件 - Put File as Binary with Ktor HttpClient in Kotlin Multiplatform Project Kotlin 多平台:在“androidMain”中使用 Android 特定库 - Kotlin Multiplatform: use Android specific libs inside 'androidMain' 服务在 Kotlin 多平台应用程序中不起作用 - Service not working in Kotlin Multiplatform Application Kotlin 多平台库接口的公开类型未解析 - Kotlin multiplatform library interface's exposed types not resolved
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM