如何在ratpack.groovy中使用ConfigData?

时间:2017-04-21 13:01:41

标签: groovy ratpack

我正在尝试按照示例Ratpacked: Using PostgreSQL Database进行操作,但我在IntelliJ IDEA中收到错误'of' in 'ratpack.config.ConfigData' can not be applied to '(groovy.lang.Closure<ratpack.config.ConfigDataBuilder>)'

ratpack {
    bindings {
        // Create generic configuration.
        final ConfigData configData = ConfigData.of { ConfigDataBuilder builder ->
            // Set configuration properties.
            // We can use the yaml, json and other
            // ConfigDataBuilder methods to read
            // configuration from other sources.
            builder.props(
                    ['postgres.user'        : 'postgres',
                     'postgres.password'    : 'secret',
                     'postgres.portNumber'  : 5432,
                     'postgres.databaseName': 'postgres',
                     'postgres.serverName'  : '192.168.99.100'])
            builder.build()
        }

        // Create instance of PostgresConfig 
        // that is used for the 
        // configurable module PostgresModule.
        bindInstance PostgresConfig, configData.get('/postgres', PostgresConfig)
        // Initialise module to create DataSource.
        module PostgresModule

        // Initialize SqlModule to provide
        // Groovy SQL support in our application.
        module SqlModule
    }
}

1 个答案:

答案 0 :(得分:2)

IntelliJ显示有关不兼容分配的检查警告。代码是有效的,当您运行应用程序时,它只是工作正常。如果检查显示为错误,您可能希望降低这些分配的报告级别。否则你需要将闭包转换为Action<ConfigDataBuilder>以使IntelliJ满意,但它也会使ratpack.groovy变得混乱。那么适当的铸造代码就是:

...
        // Create generic configuration.
        final ConfigData configData = ConfigData.of({ ConfigDataBuilder builder ->
            // Set configuration properties.
            // We can use the yaml, json and other
            // ConfigDataBuilder methods to read
            // configuration from other sources.
            builder.props(
                    ['postgres.user'        : 'postgres',
                     'postgres.password'    : 'secret',
                     'postgres.portNumber'  : 5432,
                     'postgres.databaseName': 'postgres',
                     'postgres.serverName'  : '192.168.99.100'] as Map<String, String>)
            builder.build()
        } as Action<ConfigDataBuilder>)
...