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

时间:2015-05-06 14:33:18

标签: postgresql jdbc yaml dropwizard

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

server.yml(app config file)

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

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

has an error:
  * Unrecognized field at: database
    Did you mean?:
      - metrics
      - server
      - logging

2 个答案:

答案 0 :(得分:17)

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

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

public DataSourceFactory getDataSourceFactory() {
    return database;
}

public void setDatabase(DataSourceFactory database) {
    this.database = database;
}

答案 1 :(得分:10)

在您的应用程序配置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;
    }
}

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