protobuf新增message报错:类型已存在

  问题现象:在一个已有的proto文件(RecommendResponse.proto)中新增一个message(MsgList),用maven编译proto文件时报错:

E:\workspace\wlf\wlf-msg-api>mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Detecting the operating system and CPU architecture
[INFO] ------------------------------------------------------------------------
[INFO] os.detected.name: windows
[INFO] os.detected.arch: x86_64
[INFO] os.detected.version: 10.0
[INFO] os.detected.version.major: 10
[INFO] os.detected.version.minor: 0
[INFO] os.detected.classifier: windows-x86_64
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building wlf-msg-api 1.0.18061
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ wlf-msg-api ---
[INFO] Deleting E:\workspace\wlf\wlf-msg-api\target
[INFO]
[INFO] --- protobuf-maven-plugin:0.5.0:compile (default) @ wlf-msg-api ---
[INFO] Compiling 84 proto file(s) to E:\workspace\wlf\wlf-msg-api\target\generated-sources\protobuf\java
[ERROR] PROTOC FAILED: recommend/RecommendResponse.proto:34:16: "MsgList.msgName" is already defined in file "contentrecommend/ContentRecommendResponse.proto".
recommend/RecommendResponse.proto:35:16: "MsgList.authorName" is already defined in file "contentrecommend/ContentRecommendResponse.proto".
recommend/RecommendResponse.proto:36:16: "MsgList.msgCover" is already defined in file "contentrecommend/ContentRecommendResponse.proto".
recommend/RecommendResponse.proto:37:16: "MsgList.msgUrl" is already defined in file "contentrecommend/ContentRecommendResponse.proto".
recommend/RecommendResponse.proto:33:9: "MsgList" is already defined in file "contentrecommend/ContentRecommendResponse.proto".
recommend/RecommendResponse.proto:13:19: "MsgList" seems to be defined in "contentrecommend/ContentRecommendResponse.proto", which is not imported by "recommend/RecommendResponse.proto".  To use it here, please add the necessary import.

  先看下RecommendResponse.proto:

syntax = "proto3";

option java_package = "wlf.msg.proto.base";

message RecommendResponse {
    
     Recommend data=1; 
     string messageDesc=2;
     repeated MsgList msgList= 3;

}

message Recommend{

    repeated RecommendData   buttons=1;

}

message RecommendData{

    string name=1;
    
    string url=2;    
}

message MsgList{
    string msgName=1;
    string authorName=2;
    string msgCover=3;
    string msgUrl=4;

}

  再看ContentRecommendResponse.proto:

syntax = "proto3";

option java_package = "wlf.msg.proto.base";

message  ContentRecommendResponse {

    ContentRecommendData data=1;

}
message ContentRecommendData{    
    
    string title = 1;
    
    string style = 2;

    repeated MsgList msgList=3;
    
    BiData biData = 4;
    
}
message MsgList{
    string msgName=1;
    string authorName=2;
    string msgCover=3;
    string msgUrl=4;
}

message BiData{
    
    string msisdn = 1;
}

  问题定位:从报错信息中其实已经告诉我们,在ContentRecommendResponse.proto已经存在MsgList这个message了,从上面也能看到两个proto存在同名message。

  问题解决:

  1、如果新增的MsgList跟已有的数据结构一样,那么只需要引入即可,RecommendResponse.proto改为:

syntax = "proto3";

option java_package = "wlf.msg.proto.base";
import "contentrecommend/ContentRecommendResponse.proto";

message RecommendResponse {
    
     Recommend data=1; 
     string messageDesc=2;
     repeated MsgList msgList= 3;

}

message Recommend{

    repeated RecommendData   buttons=1;

}

message RecommendData{

    string name=1;
    
    string url=2;    
}

  2、如果MsgList数据结构改了,那么就没法复用了,只能改类名,RecommendResponse.proto改为:

syntax = "proto3";

option java_package = "wlf.msg.proto.base";

message RecommendResponse {
       
     Recommend data=1; 
     string messageDesc=2;
     repeated MsgListNew msgList= 3;

}

message Recommend{

    repeated RecommendData   buttons=1;

}

message RecommendData{

    string name=1;
    
    string url=2;    
}

message MsgListNew{
    string msgCover=1;
}

  虽然类名从MsgList改为MsgListNew,但实例名没改,还是msgList,最终输出的响应字段名还是叫msgList的。

  以上两种情况修改后跑maven均可成功编译出java文件。

posted on 2018-07-19 17:07  不想下火车的人  阅读(4489)  评论(0编辑  收藏  举报

导航