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

时间:2022-08-22 10:58:14

I would like to allow clients of my endpoints to mask the fields that my web service will return. I've noticed that there is a "fields" field in the Google APIs Explorer that looks like it is meant for this feature. 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. Is this a supported feature? If so, how do I leverage it?

我想允许我的端点的客户端掩盖我的Web服务将返回的字段。我注意到Google API资源管理器中有一个“字段”字段,看起来就像是用于此功能。但是,我找不到任何关于如何从Java端点处理程序方法访问“字段”的文档,以便执行客户端的意愿并返回简化的数据集。这是支持的功能吗?如果是这样,我该如何利用它?

2 个解决方案

#1


2  

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. Returned object from your API method is processed by Jackson JSON parser which returns only fields given in fields parameter. Your object's fields should appear in field editor also.

根据我的理解,Google Cloud Endpoints调度程序会自动执行该字段屏蔽。来自API方法的返回对象由Jackson JSON解析器处理,该解析器仅返回fields参数中给出的字段。您的对象的字段也应出现在字段编辑器中。

Below is image how that field editor appears in Drive API. Your own Endpoints APIs should produce likewise field editor and Jackson should parse only fields you have selected.

下面是该字段编辑器在Drive API中的显示方式。您自己的端点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. In those cases you have to open Hibernate session manually in Endpoints. One solution I found is to inherit com.google.api.server.spi.SystemServiceServlet class and open session in 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.
  • 如果使用带有延迟获取字段的Hibernate(或其他类似框架),您可能会遇到“无法初始化代理”的错误。在这些情况下,您必须在端点中手动打开Hibernate会话。我找到的一个解决方案是继承com.google.api.server.spi.SystemServiceServlet类并在doPost -method中打开会话。该方法处理发送到端点的所有请求。您还应该使用@JsonIgnoreProperties({“hibernateLazyInitializer”,“handler”})-annotation来注释您的实体,因为Jackson将无法尝试解析这些字段。

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

#2


1  

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. Use that and it should return the specific fields only.

在Cloud Endpoints Generated Client代码中,您会发现对于您公开的每个方法,都有一个可以设置的setFields方法。使用它,它应该只返回特定的字段。

#1


2  

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. Returned object from your API method is processed by Jackson JSON parser which returns only fields given in fields parameter. Your object's fields should appear in field editor also.

根据我的理解,Google Cloud Endpoints调度程序会自动执行该字段屏蔽。来自API方法的返回对象由Jackson JSON解析器处理,该解析器仅返回fields参数中给出的字段。您的对象的字段也应出现在字段编辑器中。

Below is image how that field editor appears in Drive API. Your own Endpoints APIs should produce likewise field editor and Jackson should parse only fields you have selected.

下面是该字段编辑器在Drive API中的显示方式。您自己的端点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. In those cases you have to open Hibernate session manually in Endpoints. One solution I found is to inherit com.google.api.server.spi.SystemServiceServlet class and open session in 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.
  • 如果使用带有延迟获取字段的Hibernate(或其他类似框架),您可能会遇到“无法初始化代理”的错误。在这些情况下,您必须在端点中手动打开Hibernate会话。我找到的一个解决方案是继承com.google.api.server.spi.SystemServiceServlet类并在doPost -method中打开会话。该方法处理发送到端点的所有请求。您还应该使用@JsonIgnoreProperties({“hibernateLazyInitializer”,“handler”})-annotation来注释您的实体,因为Jackson将无法尝试解析这些字段。

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

#2


1  

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. Use that and it should return the specific fields only.

在Cloud Endpoints Generated Client代码中,您会发现对于您公开的每个方法,都有一个可以设置的setFields方法。使用它,它应该只返回特定的字段。