[英]How do I access “fields” field of API Explorer from Java Google Cloud Endpoint handler?
I would like to allow clients of my endpoints to mask the fields that my web service will return. 我想允许我的端点的客户端掩盖我的Web服务将返回的字段。 I've noticed that there is a "fields" field in the Google APIs Explorer that looks like it is meant for this feature. 我注意到Google API Explorer中有一个“字段”字段,看起来就像是用于此功能。 However, I cannot find any documentation on how I can access "fields" from my Java Endpoints handler methods in order to carry out the wishes of the client and return the reduced set of data. 但是,我找不到任何关于如何从Java端点处理程序方法访问“字段”的文档,以便执行客户端的愿望并返回简化的数据集。 Is this a supported feature? 这是支持的功能吗? If so, how do I leverage it? 如果是这样,我该如何利用它?
As there is no documentation found regarding this fields parameter I have to speak from my own experiments. 由于没有关于此字段参数的文档,我必须从我自己的实验中说出来。
In my understanding Google Cloud Endpoints dispatcher does that field masking automatically. 根据我的理解,Google Cloud Endpoints调度程序会自动执行该字段屏蔽。 Returned object from your API method is processed by Jackson JSON parser which returns only fields given in fields parameter. 来自API方法的返回对象由Jackson JSON解析器处理,该解析器仅返回fields参数中给出的字段 。 Your object's fields should appear in field editor also. 您的对象的字段也应出现在字段编辑器中 。
Below is image how that field editor appears in Drive API. 下面是该字段编辑器在Drive API中的显示方式。 Your own Endpoints APIs should produce likewise field editor and Jackson should parse only fields you have selected. 您自己的端点API应该同样生成字段编辑器,杰克逊应该只解析您选择的字段。
NOTES: 笔记:
com.google.api.server.spi.SystemServiceServlet
class and open session in doPost
-method. 我找到的一个解决方案是继承com.google.api.server.spi.SystemServiceServlet
类并在doPost
-method中打开会话。 That method handles all requests sent to Endpoints. 该方法处理发送到端点的所有请求。 You should also annotate your entities with @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
-annotation as Jackson will fail trying to parse those fields. 您还应该使用@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
注释您的实体,因为Jackson将无法尝试解析这些字段。 In the Cloud Endpoints Generated Client code, you will find that for each of the methods that you have exposed, there is a setFields
method that you can set. 在Cloud Endpoints Generated Client代码中,您会发现对于您公开的每个方法,都有一个可以设置的setFields
方法。 Use that and it should return the specific fields only. 使用它,它应该只返回特定的字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.