必需:可变可迭代<com.mycomp.adapters.grpc.x.Condicao!>!发现:可迭代

问题描述

我编写了调用 REST 端点的 gRPC 端点。此 REST 端点包含一个列表,当我尝试将其应用于 gRPC 响应时,我收到此问题主题中提到的错误。

我尝试以这种方式从列表转换为可迭代的:

MyResponse.addAllCondicoes(d.condicoes.toMutableList().asIterable())

d.condicoes 是一个列表,而 MyReponse.addAllCondicoes 需要一个 Iterable。

所以,我的直接问题是:如何在 Kotlin 中将列表转换为 Nullable (!) 和 Mutable Iterable?

其他提问方式可能是“如何将从 Rest Service 返回的列表添加到从重复消息自动生成的 gRPC 响应类型中作为原型”?

原型文件

syntax = "proto3";

option go_package = "mypackage";
option java_multiple_files = true;
option java_package = "com.mycomp.adapters.grpc.x";

package com.mycomp.adapters.grpc.x;

import "google/api/annotations.proto";

service XService {

  rpc GetX (GetMyRequest) returns (MyResponse) {
  }
}

message GetMyRequest{
    string id_cliente = 1;
}

message MyResponse {
  string id_cliente = 1;
...
  repeated Condicao condicoes = 4;
}

message Condicao{
    double pmt = 1;
    int32 prazo = 2;
}

端点

import com.mycomp.adapters.grpc.credit.Condicao
import com.mycomp.adapters.grpc.credit.XServiceGrpcKt
import com.mycomp.adapters.grpc.credit.MyResponse
import com.mycomp.adapters.grpc.credit.MyRequest
import io.grpc.Status
import io.grpc.StatusException
import javax.inject.Inject
import javax.inject.Singleton


@Singleton
class xEndpoint() : XServiceGrpcKt.XServiceCoroutineImplBase() {

    @Inject
    lateinit var httpClient:HttpClient

    override suspend fun get(request: MyRequest): MyResponse {

        val d = httpClient.get("1") //the json returned is bellow

        val MyResponse = MyResponse.newBuilder()

        if (d != null) {
            MyResponse.idCliente = d.id_cliente
            ...
            MyResponse.addAllCondicoes(d.condicoes.toMutableList().asIterable()) //*** HERE IS THE ISSUE
        }
        return MyResponse.build()
    }
}

来自其余客户端的模型回答

data class X (
        var id_cliente: String,..
       var condicoes: List<Condicao>

)

data class Condicao(
        var pmt: Double,var prazo: Int
)

Rest Endpoint 答案

[
  {
    "condicoes": [
      {
        "pmt": 100.11,"prazo": 2
      },{
        "pmt": 200.22,"prazo": 4
      },{
        "pmt": 300.33,"prazo": 10
      }
    ],"id_cliente": "1",}
]

build.gradle(主要部分)

plugins {
    id "org.jetbrains.kotlin.jvm" version "1.4.10"
    id "org.jetbrains.kotlin.kapt" version "1.4.10"
    id "org.jetbrains.kotlin.plugin.allopen" version "1.4.10"
    id "com.github.johnrengelman.shadow" version "6.0.0"
    id "io.micronaut.application" version '1.0.3'
    id "com.google.protobuf" version "0.8.12"

}

dependencies {
    implementation("io.micronaut:micronaut-validation")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut:micronaut-runtime")
    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("io.micronaut:micronaut-http-client")
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)