Getting raw HTTP Data (Headers, Cookies, etc) in Google Cloud Endpoints

百般思念 提交于 2019-11-27 04:23:11

Add an HttpServletRequest parameter to your endpoint method, e.g.

@ApiMethod
public MyResponse getResponse( HttpServletRequest req, @Named("infoId") String infoId ) {
    // Use 'req' as you would in a servlet, e.g.
    String ipAddress = req.getRemoteAddr();
    ...
}

The request is available in an Endpoints method as an injected type. An object of type HttpServletRequest is invisibly injected into your Java method definition when you declare a parameter on the method that has that type, like this:

import javax.servlet.http.HttpServletRequest;
...

@ApiMethod
public MyMethod getRequest( HttpServletRequest req ) {

HttpServletRequest myRequest = req;
...
}

This is documented here:

https://cloud.google.com/endpoints/docs/frameworks/java/parameter-and-return-types#injected_types

Quoting from the above documentation:

Injected types

Injected types are those types that receive special treatment by Cloud Endpoints Frameworks. If such a type is used as a method parameter, it isn't made a part of the API. Instead, the parameter is filled in by Endpoints Frameworks.

The injected types are the following:

com.google.appengine.api.users.User

javax.servlet.http.HttpServletRequest

javax.servlet.ServletContext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!