UTF-8 encoding of application.properties attribute

2020-02-24 12:36发布

问题:

In my application.properties I add some custom attributes.

custom.mail.property.subject-message=This is a ä ö ü ß problem

In this class I have the representation of the custom attributes.

@Component
@ConfigurationProperties(prefix="custom.mail.property")
public class MailProperties {
    private String subjectMessage;
    public String getSubjectMessage() {
        return subjectMessage;
    }
    public void setSubjectMessage(String subjectMessage) {
        this.subjectMessage = subjectMessage;
    }

And here I use my MailProperties:

@Service
public class SimpleUnknownResponseMessage extends MailProperties implements UnknownResponseMessage{

    private JavaMailSender javaMailSender;

    @Autowired
    public SimpleUnknownResponseMessage(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    @Override
    public void placeUnknownResponse(BookResponse bookResponse) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8");
            helper.setSubject(this.getSubjectMessage());            
            javaMailSender.send(message);

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

While debugging I can see that my this.getSubjectMessage() variable has this value inside: This is a ä ö ü à problem. So before sending my mail I already have an UTF-8 encoding problem.

I already checked the encoding of the application.properties file and its UTF-8.

My IDE(STS/Eclipse) and the project properties are also set on UTF-8.

How can I set the UTF-8 encoding for the text of my custom attributes in the application.properties file?

回答1:

As already mentioned in the comments .properties files are expected to be encoded in ISO 8859-1. One can use unicode escapes to specify other characters. There is also a tool available to do the conversion. This can for instance be used in the automatic build so that you still can use your favorite encoding in the source.



回答2:

Please, try to add PropertySource annotation with encoding parameter into your Configuaration file:

@PropertySource(value = "classpath:application-${env}.properties", encoding = "UTF-8")

Hope it helps.



回答3:

I've faced with the same problem. In Spring Boot there are 2 PropertySourceLoader which are used to load properties in application:

  • PropertiesPropertySourceLoader - supports UTF-8 only when load from XML
  • YamlPropertySourceLoader - supports UTF-8, but you have to change configuration format to use it

They're listed in the file https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/resources/META-INF/spring.factories

So we decided to write our own implementation of PropertySourceLoader which would be able to load properties from UTF-8 file correctly. The idea is from answer @BalusC - How to use UTF-8 in resource properties with ResourceBundle

Our PropertySourceLoader implementation:

public class UnicodePropertiesPropertySourceLoader implements PropertySourceLoader {

@Override
public String[] getFileExtensions() {
    return new String[]{"properties"};
}

@Override
public PropertySource<?> load(String name, Resource resource, String profile) throws IOException {
    if (profile == null) {
        Properties properties = new Properties();
        PropertyResourceBundle bundle = new PropertyResourceBundle(new InputStreamReader(resource.getInputStream(), "UTF-8"));
        Enumeration<String> keys = bundle.getKeys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            properties.setProperty(key, bundle.getString(key));
        }
        if (!properties.isEmpty()) {
            return new PropertiesPropertySource(name, properties);
        }
    }
    return null;
}

}

Then we created file resources/META-INF/spring.factories with content:

# Custom PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
your.own.package.UnicodePropertiesPropertySourceLoader

Now we have 3 PropertySourceLoader in our application in following order:

  • UnicodePropertiesPropertySourceLoader
  • PropertiesPropertySourceLoader
  • YamlPropertySourceLoader

NOTES!

  1. I'm not sure that it is proper usage of PropertyResourceBundle
  2. I'm not sure that order of PropertySourceLoaders in Spring Boot will be the same if you make a dedicated library to reuse it in other projects.

In our project this solution works fine.

UPDATE!

It's better to implement load method of UnicodePropertiesPropertySourceLoader without PropertyResourceBundle:

@Override
public PropertySource<?> load(String name, Resource resource, String profile) throws IOException {
    if (profile == null) {
        Properties properties = new Properties();
        properties.load(new InputStreamReader(resource.getInputStream(), "UTF-8"));
        if (!properties.isEmpty()) {
            return new PropertiesPropertySource(name, properties);
        }
    }
    return null;
}


回答4:

To set the UTF-8 encoding for the text in the application.properties (and any other Java properties as well as environment variables) add -Dfile.encoding=UTF-8 to java command line agrs.



回答5:

just converted the text with the special chars with https://native2ascii.net/