Spring-Boot 中 application.properties 属性的 UTF-8 编码 [英] UTF-8 encoding of application.properties attributes in Spring-Boot
问题描述
在我的 application.properties
中,我添加了一些自定义属性.
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;
}
这里我使用了我的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();
}
}
在调试时我可以看到我的 this.getSubjectMessage()
变量里面有这个值:这是一个 ä ö ü à 问题
.因此,在发送邮件之前,我已经遇到了 UTF-8 编码问题.
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.
我已经检查了 application.properties
文件的编码及其 UTF-8.
I already checked the encoding of the application.properties
file and its UTF-8.
我的 IDE(STS/Eclipse)和项目属性也设置为 UTF-8.
My IDE(STS/Eclipse) and the project properties are also set on UTF-8.
如何为 application.properties
文件中自定义属性的文本设置 UTF-8 编码?
How can I set the UTF-8 encoding for the text of my custom attributes in the application.properties
file?
推荐答案
正如评论中已经提到的那样,.properties 文件预计将按照 ISO 8859-1 进行编码.可以使用 unicode 转义符来指定其他字符.还有一个工具可用于执行转换.例如,这可以在自动构建中使用,以便您仍然可以在源代码中使用您喜欢的编码.
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.
这篇关于Spring-Boot 中 application.properties 属性的 UTF-8 编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!