簡體   English   中英

如何使用協議緩沖區編譯器為我的服務生成java接口而不是抽象類?

[英]how to generate java interfaces instead of abstract classes for my service using protocol buffers compiler?

如何使用協議緩沖區編譯器為我的服務生成java接口而不是抽象類?

現在我使用Gradle插件,它接受.proto文件並為我的服務而不是接口生成抽象類。 考慮到Java不允許您擴展多個類,這可能會有問題。

在完成文檔后我找不到解決方案或方法,所以任何幫助都會很棒。

我的build.gradle看起來像這樣

apply plugin: 'java'
apply plugin: 'com.google.protobuf'

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    // ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier
    // gradle versions
    classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
  }
}

repositories {
  mavenCentral()
  mavenLocal()
}

// IMPORTANT: You probably want the non-SNAPSHOT version of gRPC. Make sure you
// are looking at a tagged version of the example and not "master"!

// Feel free to delete the comment at the next line. It is just for safely
// updating the version in our release process.
def grpcVersion = '1.0.0' // CURRENT_GRPC_VERSION

dependencies {
  compile "io.grpc:grpc-netty:${grpcVersion}"
  compile "io.grpc:grpc-protobuf:${grpcVersion}"
  compile "io.grpc:grpc-stub:${grpcVersion}"
}

protobuf {
  protoc {
    // The version of protoc must match protobuf-java. If you don't depend on
    // protobuf-java directly, you will be transitively depending on the
    // protobuf-java version that grpc depends on.
    artifact = 'com.google.protobuf:protoc:3.0.0'
  }
  plugins {
    grpc {
      artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
    }
  }
  generateProtoTasks {
    all()*.plugins {
      grpc {
        // To generate deprecated interfaces and static bindService method,
        // turn the enable_deprecated option to true below:
        option 'enable_deprecated=false'
      }
    }
  }
}

// Inform IntelliJ projects about the generated code.
apply plugin: 'idea'

idea {
  module {
    // Not using generatedSourceDirs because of
    // https://discuss.gradle.org/t/support-for-intellij-2016/15294/8
    sourceDirs += file("${projectDir}/build/generated/source/proto/main/java");
    sourceDirs += file("${projectDir}/build/generated/source/proto/main/grpc");
  }

我發現它沒有生成接口的原因看起來它已被棄用,所以我不確定生成接口的方式是什么。 如果你看看我的build.gradle option 'enable_deprecated=false'將此項設置為true將生成接口但是注釋說它已被棄用,所以我不確定生成接口的新方法是什么。 我想要接口而不是抽象類。

從API的角度來看,Java接口一旦創建就必須是不可變的,以防止破壞接口的使用者和實現者。 由於向現有服務添加新方法需要與API兼容,因此grpc-java被迫刪除接口。

在Java 8中使用默認方法可能是一個選項,但grpc-java不能很快就需要Java 8,因此它可能是一種新的codegen風格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM