当前位置: 首页 > 知识库问答 >
问题:

使用DropWizard JDBI时使用自定义信用凭证

焦宏硕
2023-03-14

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

现在,我想使用“用户指定的参数”,而不是在yaml文件中具有数据库配置,我的意思是,数据库配置将通过endpointURL提供。

  • 通过dropwizard jdbi可以定制信用吗

如果是,在引用此内容时,我应该考虑在代码中进行哪些更改?-

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

我知道,在正常流程中,服务方法在运行方法中获取配置细节-

-- 配置类

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个答案

颛孙航
2023-03-14

在Spring的世界里有一种使用数据库路由器的概念(参考:https://spring.io/blog/2007/01/23/dynamic-datasource-routing/).

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

和光启
2023-03-14

为什么不直接使用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
  }
}
 类似资料:
  • 假设我有一个名为的方法的接口,它接受了的参数。在中有名为和的字段。我想模拟调用此方法的响应,并返回不同的模拟响应结果,只要ServiceRequest中的值不同。 我有以下代码,如follow(参考https://www.baeldung.com/mockito-argument-matchers),但它返回空指针异常。这里出了什么问题?

  • 问题内容: 我看到一些新网站在其网站上使用自定义字体(常规Arial,Tahoma等除外)。 并且它们支持大量的浏览器。 怎么做到的?同时,如果可能的话,还会阻止人们自由下载字体。 问题答案: 通常,您可以在CSS中使用自定义字体。这是一个非常基本的示例: 然后,简单地在特定元素上使用字体: (是您的选择器)。 请注意,某些字体格式并非在所有浏览器上都有效;您可以使用fontsquirrel.co

  • 我正在测试DynamoDB表,希望使用关键字“dev”为prod和dev环境设置不同的表名,用于开发,用于生产。 我有一个POJO 在Prod上,我希望它的名字是abc_xy_Prod_MyProjectName_Employee。 所以,我写了一个TableNameResolver 这是通过创建一个名为abc_xy_prod_MyProjectName_Employee in productio

  • 我正在使用一个自定义信用卡验证器,它具有以下条件: null 对于其他情况,我有以下regex:

  • 问题内容: 我创建了一个自定义SSLSocketFactory类,并将其设置如下 从Eclipse Dev Environment运行并在命令提示符下将其作为Jar文件运行时,它可以正常工作。但是,当我将其包装在服务包装中并作为Windows Service启动时,它不起作用。我收到以下异常, 任何帮助??? 问题答案: 自从我发布此问题以来已经很长时间了。由于这篇文章没有任何答案,而且似乎也有一

  • 包的声明和使用非常简单,在了解基本语法之后,下面通过一个案例演示在 Java 程序中声明包,以及不同包之间类的使用。 1)创建一个名为 com.dao 的包。 2)向 com.dao 包中添加一个 Student 类,该类包含一个返回 String 类型数组的 GetAll() 方法。Student 类代码如下: 3)创建 com.test 包,在该包里创建带 main() 方法的 Test 类。