Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot yml ResourceBundle file

I'm using MessageSource of Spring to load errors messages from a .properties file in classpath. My properties respect a certain "template" such as {Object}.{field}.{unrespectedConstraint} Example :

userRegistrationDto.password.Size= Le mot de passe doit avoir au minimum 6 caractères.
userRegistrationDto.email.ValidEmail= Merci de saisir une addresse mail valide.

In case of refactoring (Changing the name of the class for example), I have to change my properties file in several places.

Is there any way to use a yaml file (messages.yml) as a ResourceBundle to obtain something like :

userRegistrationDto:
  password:
    Size: Le mot de passe doit avoir au minimum 6 caractères.
  email:
    ValidEmail: Merci de saisir une addresse mail valide.
like image 594
Radouane ROUFID Avatar asked Apr 27 '17 11:04

Radouane ROUFID


People also ask

What is ResourceBundle in spring?

Spring's application context is able to resolve text messages for a target locale by their keys. Typically, the messages for one locale should be stored in one separate properties file. This properties file is called a resource bundle. MessageSource is an interface that defines several methods for resolving messages.

How do I read a yml file in spring boot?

yml file in a Spring Boot application. In a Spring Boot application, we can use properties files, YAML files, environment variables, and command-line arguments to externalize our configuration. This is useful while working with the same application code in different environments.

How do I read a yml file value?

If your key is loginUrl (inside your yaml file), you can inject its value with the @Value annotation, inside a Spring component. @Value("${loginUrl}") private String loginUrl; If it's a second level property, the path is @Value("${yourFirstKey. loginUrl}") .

Where can I find the Spring Boot yml file?

In a default structure Spring Boot web application, you can locate the application.yml file under the resources folder. To understand how Spring Boot Logging works, let’s consider an application with an empty application.yml file. model.put("message", "Hello World!");

Can @propertysource load YAML files in Spring Boot?

2. @PropertySource and YAML Format Spring Boot has great support for externalized configuration. Also, it's possible to use different ways and formats to read the properties in the Spring Boot application out-of-the-box. However, by default, @PropertySource doesn't load YAML files. This fact is explicitly mentioned in the official documentation.

How to override a YAML file in Spring Boot?

In Spring Boot, YAML files can be overridden by other YAML properties files. Prior to version 2.4.0, YAML properties were overridden by properties files in the following locations, in order of highest precedence first: Profiles' properties placed outside the packaged jar Profiles' properties packaged inside the packaged jar

What is Java ResourceBundle for YAML?

Java ResourceBundle for YAML format. Accesses YAML-formatted resources via ResourceBundle. Supports locale-specific resources according to the ResourceBundle specification. Supports YAML values nested in a map or list. Supports YAML anchors and aliases indicated by & and *. Supports multiple YAML documents separated by ---.


2 Answers

I think this should suffice for your requirements, if you need the MessageSource to be reloadable during VM operation, you might have to do a bit more digging.

@Configuration
public class TestConfig {

    @Bean(name = "testProperties")
    public Properties yamlProperties() throws IOException {
        YamlPropertiesFactoryBean bean = new YamlPropertiesFactoryBean();
        bean.setResources(new ClassPathResource("test.yml"));
        return bean.getObject();
    }

    @Bean
    public MessageSource messageSource() throws IOException {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setCommonMessages(yamlProperties());
        return messageSource;
    }
}
like image 100
Michael Hibay Avatar answered Sep 18 '22 08:09

Michael Hibay


The best solution I managed to find was found before me by @vtosh: to use this library. The only problem (but still) is that it is not popular enough.

The other option could be to extend Java localization support manually extending the ResourceBundle.Control class (you can find an official example here). But I don't see much sense in it since the library @vtosh found uses this approach.

Why there is no a solution for Spring? Well, the answer you can find in this jira. It is still in Open state so I don't expect to be any solution from their side at least for now.

like image 25
Dmitry Senkovich Avatar answered Sep 21 '22 08:09

Dmitry Senkovich