使用DropWizard JDBI时使用自定义信用卡

时间:2014-05-12 15:50:15

标签: java dropwizard jdbi

我正在使用Dropwizard JDBI框架开发Web服务。

现在,我没有在yaml文件中使用db配置,而是想使用'用户指定的参数'我的意思是,db configs将通过端点url提供。

  • 通过dropwizard jdbi可以获得自定义信用吗?

如果是的话,在提到这个时我应该在代码中做些什么改变? - >

http://dropwizard.readthedocs.org/en/latest/manual/jdbi.html

据我所知,在正常流程中,service方法在run方法中获取配置详细信息 -

- 配置类

public class ExampleConfiguration extends Configuration {
    @Valid
    @NotNull
    @JsonProperty
    private DatabaseConfiguration database = new DatabaseConfiguration();

    public DatabaseConfiguration getDatabaseConfiguration() {
        return database;
    }
}

- 服务类

@Override
        public void run(ExampleConfiguration config,
                        Environment environment) throws ClassNotFoundException {
            final DBIFactory factory = new DBIFactory();
            final DBI jdbi = factory.build(environment, config.getDatabaseConfiguration(), "postgresql");
            final UserDAO dao = jdbi.onDemand(UserDAO.class);
            environment.addResource(new UserResource(dao));
        }

- 和yaml

database:
  # the name of your JDBC driver
  driverClass: org.postgresql.Driver

  # the username
  user: pg-user

  # the password
  password: iAMs00perSecrEET

  # the JDBC URL
  url: jdbc:postgresql://db.example.com/db-prod

但在这种情况下,我可能会在资源级别获取配置详细信息...

好像 -

@GET
@Path(value = "/getProduct/{Id}/{dbUrl}/{dbUname}/{dbPass}")
@Produces(MediaType.APPLICATION_JSON)
public Product getProductById(@PathParam(value = "Id") int Id,
        @PathParam(value = "dbUrl") String dbUrl,
        @PathParam(value = "dbUname") String dbUname,
        @PathParam(value = "dbPath") String dbPass) {

     //I have to connect to the DB here! using the params i have.         
     return new Product(); //should return the Product
}

如果有人可以指点我的方向,我会很感激。

2 个答案:

答案 0 :(得分:4)

为什么不直接使用JDBI?

@GET
@Path(value = "/getProduct/{Id}/{dbUrl}/{dbUname}/{dbPass}")
@Produces(MediaType.APPLICATION_JSON)
public Product getProductById(@PathParam(value = "Id") int id,
    @PathParam(value = "dbUrl") String dbUrl,
    @PathParam(value = "dbUname") String dbUname,
    @PathParam(value = "dbPass") String dbPass) {
  DataSource ds = JdbcConnectionPool.create(dbUrl, dbUname, dbPass);
  DBI dbi = new DBI(ds);
  ProductDAO dao = dbi.open(ProductDao.class);
  Product product = dao.findById(id);
  dao.close();
  ds.dispose();
  return product;
}

@RegisterMapper(ProductMapper.class)
static interface ProductDao {
  @SqlQuery("select id from product_table where id = :id") // Whatever SQL query you need to product the product
  Product findById(@Bind("id") int id);

  @SqlQuery("select * from product_table")
  Iterator<Product> findAllProducts();
}

static class ProductMapper implements ResultSetMapper<Product> {
  public Product map(int index, ResultSet r, StatementContext ctx) throws SQLException {
    return new Product(r.getInt("id")); // Whatever product constructor you need
  }
}

答案 1 :(得分:0)

春天的世界中有一个使用数据库路由器的概念(参考:https://spring.io/blog/2007/01/23/dynamic-datasource-routing/)。

您可能可以为传递给DBI的数据库连接工厂设置代理。然后,该代理将从本地(可能)线程获取凭据,并返回真实连接,为您提供您之后的内容,并仍然允许您使用运行类型代理。

相关问题