Issue
I have a springBoot 2.4.0 app, with this piece of code in the controller:
String defaultLocation =
messages.getMessage("home.default.location", null, LocaleContextHolder.getLocale());
In the application I see the messages from the properties loaded correctly with the messages, and no error, but in the log I see this error:
14:43:41.168 [http-nio-7080-exec-13] WARN o.s.c.s.ReloadableResourceBundleMessageSource.refreshProperties 445 - Could not parse properties file [messages_en.properties]
java.util.zip.ZipException: invalid code lengths set
at java.base/java.util.zip.InflaterInputStream.read(InflaterInputStream.java:165)
at org.springframework.boot.loader.jar.ZipInflaterInputStream.read(ZipInflaterInputStream.java:52)
at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.base/java.io.InputStreamReader.read(InputStreamReader.java:185)
at java.base/java.io.Reader.read(Reader.java:229)
at java.base/java.util.Properties$LineReader.readLine(Properties.java:500)
at java.base/java.util.Properties.load0(Properties.java:415)
at java.base/java.util.Properties.load(Properties.java:378)
at org.springframework.util.DefaultPropertiesPersister.load(DefaultPropertiesPersister.java:64)
at org.springframework.context.support.ReloadableResourceBundleMessageSource.loadProperties(ReloadableResourceBundleMessageSource.java:495)
at org.springframework.context.support.ReloadableResourceBundleMessageSource.refreshProperties(ReloadableResourceBundleMessageSource.java:440)
at org.springframework.context.support.ReloadableResourceBundleMessageSource.getProperties(ReloadableResourceBundleMessageSource.java:395)
at org.springframework.context.support.ReloadableResourceBundleMessageSource.resolveCodeWithoutArguments(ReloadableResourceBundleMessageSource.java:186)
at org.springframework.context.support.AbstractMessageSource.getMessageInternal(AbstractMessageSource.java:212)
at org.springframework.context.support.AbstractMessageSource.getMessage(AbstractMessageSource.java:153)
at com.bonanza.controller.HomeController.home(HomeController.java:46)
this is my config class:
@Configuration
public class I18NConfig {
private final Environment env;
public I18NConfig(Environment env) {
this.env = env;
}
@Bean
@Qualifier("messageSource")
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource resourceBundleMessageSource = new ReloadableResourceBundleMessageSource();
resourceBundleMessageSource.setBasename("classpath:i18n/messages");
resourceBundleMessageSource.setDefaultEncoding("UTF-8"); // Set the UTF-8 encoding
resourceBundleMessageSource.setCacheSeconds(1);
return resourceBundleMessageSource;
}
}
Solution
use
org.springframework.context.support.ReloadableResourceBundleMessageSource
instead of
org.springframework.context.support.ResourceBundleMessageSource
configuration like this:
<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>classpath*:i18n/messages</value>
</property>
</bean>
or this:
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:i18n/messages");
messageSource.setCacheSeconds(10); //reload messages every 10 seconds
return messageSource;
}
Answered By - borchvm
Answer Checked By - Pedro (JavaFixing Volunteer)