问题描述
在下面的代码中,api在POST正文中获得一个“ Person” obj,
我的技术要求:
- 如果仅发送一个“个人”字段,则只能将该字段与db中的对象进行比较。
- 如果有更改,如果不只是从db返回对象,则调用save()。
- 如何将下面的代码更改为更通用和更干净的代码。是否有任何与对象类型无关的实用程序方法?
public Model.Person save(Model.Person in) {
String updateId = in.getId();
System.out.println("updateId = " + updateId);
Model.Person pExisting = get(updateId);
Model.Person.Builder outBuild = Model.Person.newBuilder(pExisting);
boolean hasChange = false;
//Change only fields that are provided in POST body.
//Todo do not compare createTs,updateTs -- they are not provided in POST to update.
if(!in.getFirstName().isEmpty()){ //Todo - Find if there is a generic util in protobuf java.
if(!Objects.equals(in.getFirstName(),pExisting.getFirstName())){
outBuild.setFirstName(in.getFirstName());
hasChange = true;
}
}
if(!in.getLastName().isEmpty()){
if(!Objects.equals(in.getLastName(),pExisting.getLastName())){
outBuild.setLastName(in.getLastName());
hasChange = true;
}
}
if(!hasChange){
System.out.println("nothing to update ... "+System.currentTimeMillis() );
return pExisting;
}
final Model.Person updatePerson = outBuild.build();
map.put(updateId,updatePerson); //Todo move to H2 or postgres later
return updatePerson;
}
样本模型
Syntax = "proto3";
import "google/protobuf/timestamp.proto";
import "google/protobuf/any.proto";
package my.model;
option java_outer_classname = "Model"; //muni.api.Model
option java_generic_services = false; //default,prevents complex generic
option java_multiple_files = false; //default behavior
message Person{
//Output only
string id=3;
string firstName = 4;
string lastName= 5;
ContactChannels contactChannels = 8;
//Output only
google.protobuf.Timestamp createTime = 1;
//Output only
google.protobuf.Timestamp updateTime = 2;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)