填充 proto 的重复元素
populate repeated element of proto
正在尝试生成 dept 数组,这是我的尝试。
原型文件
service DepartmentService {
rpc findAll(Request) returns (Response);
}
message Department {
int64 id = 1;
string name = 2;
}
message Request {
string id = 1;
}
message Response {
int32 code = 1;
repeated Department departments = 2;
}
对应java代码。
public void findAll(DepartmentOuter.Request request, StreamObserver<DepartmentOuter.Response> responseObserver) {
MongoCursor<Document> cursor = database
.getCollection("department")
.find()
.iterator();
DepartmentOuter.Response.Builder builder = DepartmentOuter.Response.newBuilder();
while (cursor.hasNext()) {
Document document = cursor.next();
DepartmentOuter.Department department = DepartmentOuter.Department
.newBuilder()
.setId(Integer.parseInt(document.getString("_id")))
.setName(document.getString("name")).build();
builder.addDepartments(department);
}
responseObserver.onNext(DepartmentOuter.Response.newBuilder().setCode(0).setDepartments(builder).build());
}
我在这段代码的最后一行遇到了问题。如何将填充输出发送到客户端?
responseObserver.onNext(DepartmentOuter.Response.newBuilder().setCode(0).setDepartments(builder).build());
编辑 - 重命名后
原型文件
service DepartmentService {
rpc findAll(Request) returns (Response);
}
message Department {
int64 id = 1;
string name = 2;
}
message Request {
string id = 1;
}
message Response {
int32 code = 1;
repeated Department department = 2;
}
@Override
public void findAll(DepartmentOuter.Request request, StreamObserver<DepartmentOuter.Response> responseObserver) {
MongoCursor<Document> cursor = database
.getCollection("department")
.find()
.iterator();
DepartmentOuter.Response.Builder builder = DepartmentOuter.Response.newBuilder();
while (cursor.hasNext()) {
Document document = cursor.next();
DepartmentOuter.Department department = DepartmentOuter.Department
.newBuilder()
.setId(Integer.parseInt(document.getString("_id")))
.setName(document.getString("name")).build();
builder.addDepartment(department);
}
responseObserver.onNext(DepartmentOuter.Response.newBuilder().setCode(0).setDepartment.build());
}
responseObserver.onNext(DepartmentOuter.Response.newBuilder().setCode(0).setDepartments(builder).build());
departments
是重复字段,所以应该是addDepartments
而不是setDepartments
。您可以调用 setDepartments(someIndex, someDepartment)
,但这仅在您之前已将至少 someIndex+1
值添加到构建器时才有效;你还没有,因为它是一个新的生成器。
但是 builder
无论如何都是 DepartmentOuter.Response.Builder
,而不是 Department
,所以这也行不通。目前尚不清楚您打算做什么;也许:
responseObserver.onNext(builder.setCode(0).build());
正在尝试生成 dept 数组,这是我的尝试。
原型文件
service DepartmentService {
rpc findAll(Request) returns (Response);
}
message Department {
int64 id = 1;
string name = 2;
}
message Request {
string id = 1;
}
message Response {
int32 code = 1;
repeated Department departments = 2;
}
对应java代码。
public void findAll(DepartmentOuter.Request request, StreamObserver<DepartmentOuter.Response> responseObserver) {
MongoCursor<Document> cursor = database
.getCollection("department")
.find()
.iterator();
DepartmentOuter.Response.Builder builder = DepartmentOuter.Response.newBuilder();
while (cursor.hasNext()) {
Document document = cursor.next();
DepartmentOuter.Department department = DepartmentOuter.Department
.newBuilder()
.setId(Integer.parseInt(document.getString("_id")))
.setName(document.getString("name")).build();
builder.addDepartments(department);
}
responseObserver.onNext(DepartmentOuter.Response.newBuilder().setCode(0).setDepartments(builder).build());
}
我在这段代码的最后一行遇到了问题。如何将填充输出发送到客户端?
responseObserver.onNext(DepartmentOuter.Response.newBuilder().setCode(0).setDepartments(builder).build());
编辑 - 重命名后
原型文件
service DepartmentService {
rpc findAll(Request) returns (Response);
}
message Department {
int64 id = 1;
string name = 2;
}
message Request {
string id = 1;
}
message Response {
int32 code = 1;
repeated Department department = 2;
}
@Override
public void findAll(DepartmentOuter.Request request, StreamObserver<DepartmentOuter.Response> responseObserver) {
MongoCursor<Document> cursor = database
.getCollection("department")
.find()
.iterator();
DepartmentOuter.Response.Builder builder = DepartmentOuter.Response.newBuilder();
while (cursor.hasNext()) {
Document document = cursor.next();
DepartmentOuter.Department department = DepartmentOuter.Department
.newBuilder()
.setId(Integer.parseInt(document.getString("_id")))
.setName(document.getString("name")).build();
builder.addDepartment(department);
}
responseObserver.onNext(DepartmentOuter.Response.newBuilder().setCode(0).setDepartment.build());
}
responseObserver.onNext(DepartmentOuter.Response.newBuilder().setCode(0).setDepartments(builder).build());
departments
是重复字段,所以应该是addDepartments
而不是setDepartments
。您可以调用 setDepartments(someIndex, someDepartment)
,但这仅在您之前已将至少 someIndex+1
值添加到构建器时才有效;你还没有,因为它是一个新的生成器。
但是 builder
无论如何都是 DepartmentOuter.Response.Builder
,而不是 Department
,所以这也行不通。目前尚不清楚您打算做什么;也许:
responseObserver.onNext(builder.setCode(0).build());