6

I am trying to find a way to set UTF-8 encoding for properties accessed via @Value annotation from application.property files in Spring boot. So far I have been successfully set encoding to my own properties sources by creating a bean:

@Bean
@Primary
public PropertySourcesPlaceholderConfigurer placeholderConfigurer(){
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setLocation(new ClassPathResource("app.properties");
    configurer.setFileEncoding("UTF-8");
    return configurer;
}

Such solution presents two problems. For once, it does NOT work with "application.properties" locations used by default by Spring Boot (http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config), and I am forced to use different file names.

And the other problem is, with it I am left with manually defining and ordering supported locations for multiple sources (eg. in jar vs outside jar properties file, etc) thus redoing a job well done already.

How would I obtain a reference to already configured PropertySourcesPlaceholderConfigurer and change it's file encoding at just the right time of application initialization?

Edit: Perhaps I am doing a mistake somewhere else? This is what causes actual problem for me: When I use application.properties to allow users to apply personal name to emails sent from an application:

@Value("${mail.mailerAddress}")
private String mailerAddress;

@Value("${mail.mailerName}")
private String mailerName;                       // Actual property is Święty Mikołaj

private InternetAddress getSender(){
    InternetAddress sender = new InternetAddress();
    sender.setAddress(mailerAddress);
    try {
        sender.setPersonal(mailerName, "UTF-8"); // Result is Święty Mikołaj
        // OR: sender.setPersonal(mailerName);   // Result is ??wiÄ?ty Miko??aj
    } catch (UnsupportedEncodingException e) {
        logger.error("Unsupported encoding used in sender name", e);
    }
    return sender;
}

When I have placeholderConfigurer bean as shown above added, and place my property inside 'app.properties' it is resoved just fine. Just renaming the file to 'application.properties' breaks it.

JockX
  • 1,928
  • 1
  • 16
  • 30
  • 1
    Before doing that, are you sure there are no simpler solutions? I use environment variables and I have used application.properties using UTF-8 without any particular issue. What is exactly the problem you are experiencing? – Alessandro Santini Jan 11 '15 at 00:49
  • @AlessandroSantini I have updated the question with particular problem it causes for me. – JockX Jan 11 '15 at 13:42
  • If you debug the class, do you see the error? It sounds more like an output problem. – chrylis -cautiouslyoptimistic- Jan 11 '15 at 14:17
  • @chrylis - Value annotated strings have different values depending on if I store property in app.propeties (accessed by my PropertySourcesPlaceholderConfigurer) or application.propertes (spring default). Doesn't look like an output problem. – JockX Jan 11 '15 at 19:29
  • Did you try encoding the string with http://docs.oracle.com/javaee/6/api/javax/mail/internet/MimeUtility.html#encodeText%28java.lang.String%29 ? I did not have the time to check whether InternetAddress does it under the hood, but the class I quoted does an RFC-2047 compliant translation of the name (which apparently needs to be made of US-characters only, unless I misunderstood the specs). – Alessandro Santini Jan 11 '15 at 22:26

3 Answers3

9

Apparently properties loaded by Spring Boot's ConfigFileApplicationListener are encoded in ISO 8859-1 character encoding, which is by design and according to format specification.

On the other hand, the .yaml format supports UTF-8 out of the box. A simple extension change fixes the problem for me.

JockX
  • 1,928
  • 1
  • 16
  • 30
  • But [JDK 9 supports properties files in UTF-8](https://docs.oracle.com/javase/9/intl/internationalization-enhancements-jdk-9.htm#JSINT-GUID-5ED91AA9-B2E3-4E05-8E99-6A009D2B36AF). Has the latest Spring Boot been updated to match? – Garret Wilson Aug 23 '20 at 22:05
0

@JockX suggestion works perfectly. Also, the conversion from property to yaml is quite straightforward. This:

spring.main.web_environment=false
email.subject.text=Here goes your subject
email.from.name=From Me
email.from.address=me@here.com
email.replyTo.name=To Him
email.replyTo.address=to@him.com

Would become:

spring:
  main:
    web_environment: false
email:
  subject:
    text: Here goes your subject
  from:
    name: From Me
    address: me@here.com
  replyTo:
    name: To Him
    address: to@him.com
cristianoms
  • 3,456
  • 3
  • 26
  • 28
0

Another approach would be Instead of renaming the complete file from .properties to .yml you can pick the props which need UTF-8 support and move them to .yml file. This way you need not rewrite ur .properties file.

I advice this because if you have props like

my.string.format= %s-hello-%s

This breaks in .yml files. You would have to write them as

my.string.format: |
   %s-hello-%s

Which then leads to adding a new line in the property valye my.string.format when read in the Java code.

MohamedSanaulla
  • 6,112
  • 5
  • 28
  • 45