DEV Community

Cover image for How To Handle Multiple Message Properties In Spring Boot
javatodev
javatodev

Posted on • Originally published at javatodev.com

1

How To Handle Multiple Message Properties In Spring Boot

In this tutorial, I'm going to explain how we can use multiple message properties files in a spring boot application. We can use this approach when we need to differentiate message properties into multiple files.

Just think you have a requirement of keeping messages error messages, API responses in different message property files. We can use the following approach to keep errors and API responses in different files.

Technologies Going to Use,

  • Java 1.8
  • Spring Boot: 2.3.4.RELEASE
  • Lombok
  • Gradle
  • Intellij Idea for IDE

Create a Spring Boot Application

Here I'm using spring initializr to generate a spring boot project with all the dependencies I need for this tutorial.

If you are really new to Spring Boot, Please follow our article on How to Create a Spring Boot Project.

Here I've selected the following dependencies to create spring boot project using spring initilizr,

  • Spring Web  -  contains common web-specific utilities for both Servlet and Portlet environments.
  • Lombok - The coolest plugin to spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully-featured builder, Automate your logging variables, and much more.

If you need to learn how we can use Lombok in spring boot follow our article Guide to use Lombok In Spring Boot.

Alt Text

Adding Message Properties Into The Spring Boot Project

Here I'm creating 2 message property files in resources, to keep errors and API responses. In order to do that, just create two files in src/main/resources/messages folder with naming as below,

  • api_error_messages.properties
  • api_response_messages.properties

Alt Text

Then add following message key and value pairs into those files,

  • api_error_messages.properties,
api.error.user.not.found=Requested User Not Found In Registry. Please check and retry
api.error.user.already.registered=Username already registered. Please use different username and retry.
  • api_response_messages.properties,
api.response.user.creation.successful=User registered successfully inside the application.
api.response.user.update.successful=User update request successfully applied.

Setup MessageSources Bean With Introducing Multiple Message Properties

Now we have our error message and API response property files inside the project. Let's introduce those files to the Spring Boot application using message source bean.

Here I'm using separate @Configuration class to set up this MessageSource bean, You can use the same approach or use already available configuration class to define this MessageSource Bean.

package com.javatodev.api.config;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
@Configuration
public class AppConfiguration {
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource
= new ReloadableResourceBundleMessageSource();
messageSource.setBasenames(
"classpath:/messages/api_error_messages",
"classpath:/messages/api_response_messages"
);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}

Now we can access these message properties inside our application anywhere we need. Here I'll demonstrate it with @PostConstruct method in Main Class,

package com.javatodev.api;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.MessageSource;
import javax.annotation.PostConstruct;
import java.util.Locale;
@SpringBootApplication
@RequiredArgsConstructor
public class SpringBootMultipleMessageSourcesApplication {
private final MessageSource messageSource;
public static void main(String[] args) {
SpringApplication.run(SpringBootMultipleMessageSourcesApplication.class, args);
}
@PostConstruct
public void postConstruct() {
System.out.println("Running Message Property Data");
System.out.println(messageSource.getMessage("api.error.user.not.found", null, Locale.ENGLISH));
System.out.println(messageSource.getMessage("api.error.user.already.registered", null, Locale.ENGLISH));
System.out.println(messageSource.getMessage("api.response.user.creation.successful", null, Locale.ENGLISH));
System.out.println(messageSource.getMessage("api.response.user.update.successful", null, Locale.ENGLISH));
System.out.println("End Message Property Data");
}
}

Start the application and output should look like below.

Alt Text

Conclusion

Thanks for reading our latest article on How To Handle Multiple Message Properties In Spring Boot with practical usage.

If you are looking for spring boot based practical application development tutorials, just check our article series.

You can get source code for this tutorial from our GitHub repository.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay