简体   繁体   English

如何从Java Google Cloud Endpoint处理程序访问API Explorer的“字段”字段?

[英]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: 笔记:

  • Appengine devserver (localhost) shows that field editor too but somehow does not take selected fields in account when parsing to JSON. Appengine devserver(localhost)也显示了字段编辑器,但在解析为JSON时,某种程度上不会在帐户中选择所选字段。
  • In some cases field editor allows you to select fields in nested objects, in some cases not. 在某些情况下,字段编辑器允许您选择嵌套对象中的字段,在某些情况下不允许。 I have not found out the logic behind this behaviour. 我还没有发现这种行为背后的逻辑。
  • If using Hibernate (or some other similar framework) with lazy-fetched fields you will probably face "could not initialize proxy" -error. 如果使用带有延迟获取字段的Hibernate(或其他类似框架),您可能会遇到“无法初始化代理”的错误。 In those cases you have to open Hibernate session manually in Endpoints. 在这些情况下,您必须在端点中手动打开Hibernate会话。 One solution I found is to inherit 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将无法尝试解析这些字段。

Drive API,字段编辑器

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.

相关问题 是否可以在API Explorer中向Cloud Endpoint Java字段添加描述? - Is it possible to add a description to the Cloud Endpoint Java fields in the API Explorer? Google Cloud Endpoint - 如何创建处理程序以从cron调用端点 - Google Cloud Endpoint - How to create handler to call endpoints from cron 我如何坚持Google Cloud Endpoint对sharedPreference的响应 - How do I persist google cloud endpoint response to sharedPreference 如何从Java中的类型参数访问字段? - How do I access fields from a type parameter in Java? Google Cloud Endpoint上的API密钥授权的Java注释? - Java Annotations for API-key authorization on Google Cloud Endpoint? Google Cloud Endpoint不在端点列表中创建Api - Google Cloud Endpoint Not Creating Api in Endpoint List Java 如何访问文本字段 - Java how do I access text field 运行Google端点api资源管理器时出错 - error running google endpoint api explorer 如何使用 Google Cloud DNS API 从项目中获取所有“managerzone”或从“managerzone”中获取“记录”? - How do I get all the 'managerzone' from the project or 'record' from the 'managerzone' using the Google Cloud DNS API? 来自 Java 的谷歌云端点框架的配额问题 - Problem with quotas from google cloud endpoint framework for Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM