java - How can I access Dropwizard Resource directly, and not via REST -


i've create dropwizard resource , mapped rest api. want reuse resource api other points in code java api. how can it?

this resource class:

@path("/providers_new") public class providerresource {     private providerdao dao;      public providerresource(providerdao dao) {         this.dao = dao;     }      @get     @path("/get")     @produces("application/json")     public list<provider> getall() {         return dao.getallproviders();     } } 

please note providerresource initialized dao:

public class entitiesservice extends service<entitiesserviceconfiguration> {     public static void main(string[] args) throws exception {         new entitiesservice().run(args);     }      @override     public void initialize(bootstrap<entitiesserviceconfiguration> bootstrap) {         bootstrap.setname("entities");         ...     }      @override     public void run(entitiesserviceconfiguration configuration,                 environment environment) throws classnotfoundexception {         final dbifactory factory = new dbifactory();         final dbi jdbi = factory.build(environment, configuration.getdatabaseconfiguration(), "my_db");         final providerdao dao = jdbi.ondemand(providerdao.class);         environment.addresource(new providerresource(dao));         ...     } } 

now providerresource on air, use code. like:

arraylist<provider> providers = idontknowhowtogetproviderresource.getall(); 

what say?

i think question wrong design (and why not trivial use resource out of other resource).

the resources layer mapping rest api urls methods. these methods hold logic of actions want implement. design write these logic in separated services (java classes not "dropwizard services").

these services classes better initialized once in run() method, , passed relevant resources constructor dependency. in way can create services logic reused in different resources , each resource hold dependencies (same dao in question code example)


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -