Xegara
Xegara

Reputation: 131

How to convert properties to JSON the Spring MVC way?

I need my web service to serve me the messages.properties which contains localized texts in JSON format. I know that I can write my own parser to do that but where should I insert this logic in the Spring framework? Or is there a Spring infrastructure feature that already can do that?

Upvotes: 3

Views: 1542

Answers (1)

Bogdan Oros
Bogdan Oros

Reputation: 1299

You can use @PropertySource annotation on your class to load your property file into memory.

@Configuration
class MessagesConfiguration {

    @Bean(name = "messageProperties")
    public static PropertiesFactoryBean mapper() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new ClassPathResource("messages.properties"));
        return bean;
    }

    @Resource(name="messageProperties")
    private Properties messages = new Properties();

    public Properties getMessages() {
        return messages;
    }

}

Properties.class is just a wrapper for Map<String, String> so you can convert it to JSON.

Upvotes: 2

Related Questions