Access Request object from REST

后端 未结 2 1046
傲寒
傲寒 2020-12-04 18:31

Is it possible to access the Request object in a REST method under JAX-RS?

I just found out

@Context Request request;
相关标签:
2条回答
  • 2020-12-04 18:58

    On JAX-RS you must annotate a Request parameter with @Context:

     @GET  
     public Response foo(@Context Request request) {
    
     }
    

    Optionally you can also inject:

    • UriInfo
    • HttpHeaders
    • SecurityContext
    • HttpServletRequest
    0 讨论(0)
  • 2020-12-04 19:14

    To elaborate on @dfa's answer for alternatives, I find this to be simpler than specifying the variable on each resource method signature:

    public class MyResource {
    
      @Context
      private HttpServletRequest httpRequest;
    
      @GET  
      public Response foo() {  
        httpRequest.getContentType(); //or whatever else you want to do with it
      }
    }
    
    0 讨论(0)
提交回复
热议问题