必选:generatedmessagev3.builder< *>:如何用值初始化com.google.type.money

v9tzhpje  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(384)

上下文:我想设计一个包含货币字段的原型文件,该字段将在grpc服务响应中使用。我正在努力学习这个教程
我得到这个错误

Type mismatch.
Required:
GeneratedMessageV3.Builder<*>!
Found:String

它明确地说我必须使用generatedmessagev3.builder,但我不知道如何使用它。
这是原稿

syntax = "proto3";

    package com.mycomp.adapters.grpc.test;

    import "google/api/annotations.proto";
    import "google/type/money.proto";

    service TestService {

      rpc GetTest (GetTestRequest) returns (Test) {
      }
    }

    message GetTestRequest{
        string id_cliente = 1;
    }

    message Test {
      string id_cliente = 1;
      google.type.Money test_money = 2;
    }

我是如何实现服务的,我的问题是如何初始化一个非常简单的com.google.type.money变量。

import com.google.type.Money
...other imports

@Singleton
class TestEndpoint() : TestServiceGrpcKt.TestServiceCoroutineImplBase() {

    override suspend fun getTest(request: GetTestRequest): Test {

        val test = Test.newBuilder()

        ...

        test.testMoney = Money("999.99") //***certainly my mistake is here

        return test.build()
    }

如果相关的话,下面是build.gradle最重要的部分

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")

    implementation ("com.google.api.grpc:proto-google-common-protos:1.0.0")
}

我知道在proto文件中导入“google/api/annotations.proto”和在kotlin中导入com.google.type.money所需要的唯一依赖是

com.google.api.grpc:proto-google-common-protos:1.0.0
4xrmg8kj

4xrmg8kj1#

如果我正确地解释了javadoc,您就不能示例化 Money 直接上课。相反,使用类似于:

test.testMoney = Money.newBuilder()
    .setCurrencyCode("USD")
    .setUnits(999)
    .build();

相关问题