regisd
regisd

Reputation: 151

How to get the servlet context in a Google Cloud Endpoint method?

I have a simple Google cloud endpoint. How can I access the context? I'm looking for something similar to getServletContext() from HttpServlet.

@Api
public FooEndpoint {
    @ApiMehod
    public String[] getFiles() {
        // TODO: return files in WEB-INF/data
    }
}

Upvotes: 2

Views: 286

Answers (1)

Elliotte Rusty Harold
Elliotte Rusty Harold

Reputation: 991

The servlet context is an injected type. Just include it as a method argument to an endpoint method and the server will fill it in for you. E.g.

@Api
public FooEndpoint {
  @ApiMethod
  public String[] getFiles(ServletContext context) {
    // TODO: return files in WEB-INF/data
  }
}

You don't need to annotate the argument, and it won't appear in the method stub in the generated client library.

Upvotes: 3

Related Questions