Rest easy and init params - how to access?

后端 未结 1 662
梦谈多话
梦谈多话 2021-01-18 08:12

I\'d like to have some init params in my web.xml and retrieve them later in the application, I know I can do this when I have a normal servlet. However with resteasy I confi

1条回答
  •  醉梦人生
    2021-01-18 09:00

    Use the @Context annotation to inject whatever you want into your method:

    @GET
    public Response getWhatever(@Context ServletContext servletContext) {
       String myParm = servletContext.getInitParameter("parmName");
    }
    

    With @Context you can inject HttpHeaders, UriInfo, Request, HttpServletRequest, HttpServletResponse, ServletConvig, ServletContext, SecurityContext.

    Or anything else if you use this code:

    public class MyApplication extends Application {
      public MyApplication(@Context Dispatcher dispatcher) {
        MyClass myInstance = new MyClass();
        dispatcher.getDefautlContextObjects().
             put(MyClass.class, myInstance);
      }
    }
    
    @GET
    public Response getWhatever(@Context MyClass myInstance) {
       myInstance.doWhatever();
    }
    

    0 讨论(0)
提交回复
热议问题