Interface ConfigData

  • All Known Subinterfaces:
    ServerConfig

    public interface ConfigData
    Configuration data for the application, potentially built from many sources.

    A ConfigData object allows configuration to be “read” as Java objects. The data used to populate the objects is specified when building the configuration data. The static methods of this interface can be used to build a configuration data object.

    
     import com.google.common.collect.ImmutableMap;
     import ratpack.test.embed.EmbeddedApp;
     import static org.junit.jupiter.api.Assertions.*;
    
     public class Example {
       public static class MyAppConfig {
         private String name;
    
         public String getName() {
           return name;
         }
       }
    
       public static void main(String[] args) throws Exception {
         EmbeddedApp.of(s -> s
           .serverConfig(c -> c
             .props(ImmutableMap.of("server.publicAddress", "http://app.example.com", "app.name", "Ratpack"))
             .sysProps()
             .require("/app", MyAppConfig.class)
           )
           .handler(registry ->
             ctx -> ctx.render("Hi, my name is " + ctx.get(MyAppConfig.class).getName() + " at " + ctx.getServerConfig().getPublicAddress())
           )
         ).test(httpClient ->
           assertEquals("Hi, my name is Ratpack at http://app.example.com", httpClient.getText())
         );
       }
     }
     
    See Also:
    ConfigDataBuilder
    • Method Detail

      • of

        static ConfigData of​(Environment env,
                             Action<? super ConfigDataBuilder> definition)
                      throws Exception
        Builds a new config data with the default object mapper, from the given definition.

        The default object mapper is constructed without argument. It then has the following Jackson modules applied implicitly:

        The DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES feature is disabled.

        The following features of the JSON factory are enabled:

        • JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES
        • JsonParser.Feature.ALLOW_SINGLE_QUOTES
        Parameters:
        env - the environment provider for configuration purposes
        definition - the config data definition
        Returns:
        a config data
        Throws:
        Exception - any thrown by building the config data
      • of

        static ConfigData of​(Environment env,
                             ObjectMapper objectMapper,
                             Action<? super ConfigDataBuilder> definition)
                      throws Exception
        Builds a new config data with the specified object mapper, from the given definition.

        The of(Action) method should be favoured, as it applies useful default configuration to the object mapper used.

        Parameters:
        env - the environment provider for configuration purposes
        objectMapper - the object mapper to use for configuration purposes
        definition - the config data definition
        Returns:
        a config data
        Throws:
        Exception - any thrown by building the config data
      • get

        default <O> O get​(String pointer,
                          Class<O> type)
        Binds a segment of the configuration data to the specified type.
        Type Parameters:
        O - the type to bind to
        Parameters:
        pointer - a JSON Pointer specifying the point in the configuration data to bind from
        type - the class of the type to bind to
        Returns:
        an instance of the specified type with bound configuration data
      • getAsConfigObject

        default <O> ConfigObject<O> getAsConfigObject​(String pointer,
                                                      Class<O> type)
        Binds a segment of the configuration data to the specified type.
        Type Parameters:
        O - the type to bind to
        Parameters:
        pointer - a JSON Pointer specifying the point in the configuration data to bind from
        type - the class of the type to bind to
        Returns:
        a config object of the specified type with bound configuration data
      • getAsConfigObject

        <O> ConfigObject<O> getAsConfigObject​(String pointer,
                                              com.google.common.reflect.TypeToken<O> type)
        Binds a segment of the configuration data to the specified type.
        Type Parameters:
        O - the type to bind to
        Parameters:
        pointer - a JSON Pointer specifying the point in the configuration data to bind from
        type - the class of the type to bind to
        Returns:
        a config object of the specified type with bound configuration data
        Since:
        1.4
      • get

        default <O> O get​(Class<O> type)
        Binds the root of the configuration data to the specified type.
        Type Parameters:
        O - the type to bind to
        Parameters:
        type - the class of the type to bind to
        Returns:
        an instance of the specified type with bound configuration data