[英]UTF-8 encoding of application.properties attributes in Spring-Boot
In my application.properties
I add some custom attributes. 在我的application.properties
我添加了一些自定义属性。
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
: 在这里我使用我的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
. 在调试时我可以看到我的this.getSubjectMessage()
变量里面有这个值: This is a ä ö ü à problem
。 So before sending my mail I already have an UTF-8 encoding problem. 所以在发送邮件之前我已经有了UTF-8编码问题。
I already checked the encoding of the application.properties
file and its UTF-8. 我已经检查了application.properties
文件及其UTF-8的编码。
My IDE(STS/Eclipse) and the project properties are also set on UTF-8. 我的IDE(STS / Eclipse)和项目属性也在UTF-8上设置。
How can I set the UTF-8 encoding for the text of my custom attributes in the application.properties
file? 如何在application.properties
文件中为自定义属性的文本设置UTF-8编码?
As already mentioned in the comments .properties files are expected to be encoded in ISO 8859-1. 正如评论中已经提到的那样.properties文件应该在ISO 8859-1中编码。 One can use unicode escapes to specify other characters. 可以使用unicode转义来指定其他字符。 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. 例如,这可以在自动构建中使用,这样您仍然可以在源中使用您喜欢的编码。
Please, try to add PropertySource
annotation with encoding parameter into your Configuaration file: 请尝试将带有编码参数的PropertySource
注释添加到Configuaration文件中:
@PropertySource(value = "classpath:application-${env}.properties", encoding = "UTF-8")
Hope it helps. 希望能帮助到你。
I've faced with the same problem. 我遇到了同样的问题。 In Spring Boot there are 2 PropertySourceLoader which are used to load properties in application: 在Spring Boot中有2个PropertySourceLoader,用于在应用程序中加载属性:
They're listed in the file https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/resources/META-INF/spring.factories 它们列在文件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. 所以我们决定编写自己的PropertySourceLoader实现,它可以正确地从UTF-8文件加载属性。 The idea is from answer @BalusC - How to use UTF-8 in resource properties with ResourceBundle 这个想法是从@BalusC回答 - 如何在ResourceBundle的资源属性中使用UTF-8
Our PropertySourceLoader implementation: 我们的PropertySourceLoader实现:
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: 然后我们用内容创建了文件资源/ META-INF / spring.factories :
# Custom PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
your.own.package.UnicodePropertiesPropertySourceLoader
Now we have 3 PropertySourceLoader in our application in following order: 现在我们的应用程序中有3个PropertySourceLoader,顺序如下:
NOTES! 笔记!
In our project this solution works fine. 在我们的项目中,此解决方案正常。
UPDATE! UPDATE!
It's better to implement load method of UnicodePropertiesPropertySourceLoader without PropertyResourceBundle: 最好在没有PropertyResourceBundle的情况下实现UnicodePropertiesPropertySourceLoader的加载方法:
@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;
}
要为application.properties(以及任何其他Java属性以及环境变量)中的文本设置UTF-8编码,请将-Dfile.encoding=UTF-8
添加到java命令行agrs。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.