我创建了 Dropwizard 资源并将其映射到 REST API。现在我想从我的代码中的其他点重用这个资源 API 作为 JAVA API。我该怎么做?
这是资源类:
@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();
}
}
请注意,ProviderResource 是用 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));
...
}
}
现在 ProviderResource 正在播出,我想从我的代码中使用它。就像是:
ArrayList<Provider> providers = iDontKnowHowToGetProviderResource.getAll();
你说什么?