Search code examples
javarestglassfishjax-rs

How to get the HTTP method of a REST request


In case of an exception in my Java REST application I would like to log various information on causing HTTP request.

I can obtain the URI of the request and the HTTP headers via context injection

@Context
private UriInfo uriInfo;

@Context
private HttpHeaders headers;

But how can I obtain the HTTP method (GET, PUT, ...)?


Solution

  • I use Jersey. Don't know if this applies for you but ... :

    import javax.servlet.http.HttpServletRequest;    
    
    @Context final HttpServletRequest request
    

    The Request class has the method getMethod(). It returns the used HTTP method.