*无法识别的字段:数据库您的意思是?: - metrics - server - logging - DROPWIZARD

Rad*_*dek 6 postgresql yaml jdbc dropwizard

在我的应用程序配置文件(server.yml)中添加数据库详细信息后,我无法启动我的dropwizard应用程序.

server.yml(app配置文件)

server:
  applicationConnectors:
  - type: http 
    port: 8080
  adminConnectors:
  - type: http
    port: 9001

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

  # the username
  user: dbuser

  # the password
  password: pw123

  # the JDBC URL
  url: jdbc:postgresql://localhost/database

  # any properties specific to your JDBC driver:
  properties:
    charSet: UTF-8

  # the maximum amount of time to wait on an empty pool before throwing an exception
  maxWaitForConnection: 1s

  # the SQL query to run when validating a connection's liveness
  validationQuery: "/* MyService Health Check */ SELECT 1"

  # the timeout before a connection validation queries fail
  validationQueryTimeout: 3s

  # the minimum number of connections to keep open
  minSize: 8

  # the maximum number of connections to keep open
  maxSize: 32

  # whether or not idle connections should be validated
  checkConnectionWhileIdle: false

  # the amount of time to sleep between runs of the idle connection validation, abandoned cleaner and idle pool resizing
  evictionInterval: 10s

  # the minimum amount of time an connection must sit idle in the pool before it is eligible for eviction
  minIdleTime: 1 minute
Run Code Online (Sandbox Code Playgroud)

作为运行dropwizard应用程序的结果,我可以看到:

has an error:
  * Unrecognized field at: database
    Did you mean?:
      - metrics
      - server
      - logging
Run Code Online (Sandbox Code Playgroud)

小智 19

除了dropwizard示例给出的代码之外,您还需要为数据库属性添加setter.

@Valid
@NotNull
@JsonProperty("database")
private DataSourceFactory database = new DataSourceFactory();

public DataSourceFactory getDataSourceFactory() {
    return database;
}

public void setDatabase(DataSourceFactory database) {
    this.database = database;
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!它有效,此答案应标记为正确 (2认同)

Dav*_*nce 11

在应用程序配置java文件中,您必须为"database"添加匹配属性.如果您指定的属性是标准属性(它们看起来很好,那么!)那么您可以保持DataSourceFactory类型:

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

    public DataSourceFactory getDataSourceFactory() {
        return database;
    }

    public void setDatabase(DataSourceFactory database) {
        this.database = database;
    }
}
Run Code Online (Sandbox Code Playgroud)

示例:http://www.dropwizard.io/0.9.0/docs/manual/jdbi.html