java - Passing parameters to resource using embedded Jetty -
i have resource want expose has constructor parameters injection. i'm not using injection framework, , i've got jetty embedded.
right jetty scans resources this:
servletcontexthandler servletcontexthandler = new servletcontexthandler(server, "/server"); servletholder jerseyservletholder = new servletholder(servletcontainer.class); jerseyservletholder.setinitparameter("com.sun.jersey.config.property.packages", "my.package.to.scan"); servletcontexthandler.addservlet(jerseyservletholder, "/*");
but prevents me injecting in constructor, have use default constructor.
i'd specify own instance pass resource constructor. there way instantiate resource manually , add servlet container?
is possible :
myrestresource resource = new myrestresource(param1, param2); servletcontexthandler.addservlet(resource);
or that?
if myrestresource
servlet
, can use existing servlet holder constructor.
servletcontexthandler servletcontexthandler = new servletcontexthandler(server, "/server"); servletholder jerseyservletholder = new servletholder(servletcontainer.class); jerseyservletholder.setinitparameter("com.sun.jersey.config.property.packages", "my.package.to.scan"); servletcontexthandler.addservlet(jerseyservletholder, "/*"); myrestresource resource = new myrestresource(param1, param2); // wrap resource in servletholder servletholder resourceholder = new servletholder(resource); // assign resource servlet pathspec servletcontexthandler.addservlet(resourceholder, "/rest");
if have servlet, , want access setup @ startup time, use contexthandler.setattribute(string, object) , servletcontext.getattribute(string) techniques.
in server startup
servletcontexthandler servletcontexthandler = new servletcontexthandler(server, "/server"); servletholder jerseyservletholder = new servletholder(servletcontainer.class); jerseyservletholder.setinitparameter("com.sun.jersey.config.property.packages", "my.package.to.scan"); servletcontexthandler.addservlet(jerseyservletholder, "/*"); myrestresource resource = new myrestresource(param1, param2); // set resource context attribute servletcontexthandler.setattribute("rest-1", resource);
then access later servlet api
servletcontext ctx = servletrequest.getservletcontext(); myrestresource resource = (myrestresource) ctx.getattribute("rest-1");
Comments
Post a Comment