Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UTF-8 encoding of application.properties attributes in Spring-Boot

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?

like image 976
Patrick Avatar asked May 25 '16 12:05

Patrick


People also ask

Does spring boot support application properties support?

Spring Boot supports different properties based on the Spring active profile. For example, we can keep two separate files for development and production to run the Spring Boot application.

What is application properties in spring boot?

So in a spring boot application, application. properties file is used to write the application-related property into that file. This file contains the different configuration which is required to run the application in a different environment, and each environment will have a different property defined by it.

How do I change the encoding of a properties file in eclipse?

properties files are Latin1 (ISO-8859-1) encoded by definition. ISO-8859-1 as its default encoding. You can change this under: Preferences > General > Content Types.

Is Java UTF-8 or 16?

The native character encoding of the Java programming language is UTF-16. A charset in the Java platform therefore defines a mapping between sequences of sixteen-bit UTF-16 code units (that is, sequences of chars) and sequences of bytes.


1 Answers

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.

like image 123
Henry Avatar answered Sep 22 '22 13:09

Henry