Google Cloud Endpoints Tips #3 : Understand Injected Types
December 2017: Please note that this post has not been updated for a while and there could be differences in terms of commands, screenshots and problems in running the code.
Typically, when you design your API and the associated methods, you do spend some time deciding what parameters will be passed for each API Method.
It pays to know about Injected Types, which are parameters that are given a special treatment by the Cloud Endpoints framework. These parameters when defined in the method signature will not need to be passed explicitly while making the call. Instead it will be the Cloud Endpoints framework that will populate it for you. You can then make sure of this inside of your method implementation.
As per the documentation, Cloud Endpoints supports the following 3 injected types:
- com.google.appengine.api.users.User
- javax.servlet.http.HttpServletRequest
- javax.servlet.ServletContext
The first one is used if you secure your API Endpoint. The User object will be passed to you. Always remember to check the user object for Null value and do your own validations as needed.
The next 2 injected types are very useful, especially if you wish to dig into the details of the HTTP Request , Servlet Context and do some investigation yourself. These two parameters come handy if your API method implementation is tightly coupled with the Web Framework. Maybe that is a hint to revisit your API :-)
List of Cloud Endpoints Tips