我可以将 google-http-client json 请求/响应对象的字段标记为只读吗?

问题描述

我正在使用一个 API,该 API 在 JSON 响应中具有许多只读字段。如果您尝试使用负载中的任何这些字段更新资源,则会出现错误

public class SomeThing {
    @Key public int id;
    @Key("some_readonly_field") public String someReadOnlyField;
    @Key("writeable_field") public String writeableField;
}

// Get some resource
SomeThing model = httpResponse.parseAs(SomeThing.class);


// Some time later we want to update that resource
model.writeableField = "new value";
HttpContent content = new JsonHttpContent(new GsonFactory(),model);
HttpRequest request = requestFactory.buildrequest(HttpMethods.PUT,requestUrl,content)

request.execute(); //Server gets mad as the payload containing "some_readonly_field"

有什么方法可以将键标记为只读,以便我可以对请求内容和解析的响应使用相同的对象?

如果没有,最好的做法是什么?

我是否应该拥有具有相似但不同的字段集的单独请求和响应模型(例如 SomeThingRequest、SomeThingResponse)? 我应该序列化到地图或原始对象的副本并删除或清除某些只读字段吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)