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()
变量在内部具有以下值:This is a ä ö ü à problem
.因此,在发送邮件之前,我已经遇到了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屋!