简体   繁体   中英

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

I've create Dropwizard Resource and mapped it to REST API. Now I want to reuse this Resource API from other points in my code as a JAVA API. How can I do it?

This is the 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 that ProviderResource is initialized with 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 that ProviderResource is on the air, I would like to use it from my code. Something like:

ArrayList<Provider> providers = iDontKnowHowToGetProviderResource.getAll();

What do you say?

I think that my question is basically wrong design (and this is why is it not trivial to use resource out of other resource).

The Resources layer is for mapping REST API URLs into methods. These methods hold the logic of the actions that we want to implement. A good design will be to write these logic in separated Services (Java classes not "DropWizard Services").

These Services classes are better to be initialized once in run() method, and be passed to the relevant Resources as a constructor dependency. In this way we can create services with logic to be reused in different Resources and each Resource will hold its dependencies (same like the dao in my question code example)

I can see a couple of approaches:

  1. You manually create a ProviderDao and construct your ProviderResource(dao) with the dao you create.
  2. You create an HttpClient object to make an HTTP call to your resource while it's running. For this, you could use Dropwizard's client libraries
  3. You can create a mock call and response (this will only validate the Resource and not the DAO).
  4. You can specifically test the DAO.

From the JDBI documentation, you can test like this:

DBI dbi = new DBI("jdbc:h2:mem:test");
YourDAO dao = dbi.open(YourDAO.class);

// Test something
dao.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM