Java Spring-Boot 中 application.properties 属性的 UTF-8 编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37436927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UTF-8 encoding of application.properties attributes in Spring-Boot
提问by Patrick
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
. So before sending my mail I already have an UTF-8 encoding problem.
在调试时,我可以看到我的this.getSubjectMessage()
变量里面有这个值:This is a ?¤ ?? ?? ? 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?
如何为文件中自定义属性的文本设置 UTF-8 编码application.properties
?
采纳答案by Henry
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 toolavailable 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.
正如评论中已经提到的,.properties 文件预计将在 ISO 8859-1 中编码。可以使用 unicode 转义来指定其他字符。还有一个工具可用于进行转换。例如,这可以用于自动构建,以便您仍然可以在源代码中使用您喜欢的编码。
回答by merz
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 用于在应用程序中加载属性:
- PropertiesPropertySourceLoader- supports UTF-8 only when load from XML
- YamlPropertySourceLoader- supports UTF-8, but you have to change configuration format to use it
- PropertiesPropertySourceLoader- 仅在从 XML 加载时支持 UTF-8
- YamlPropertySourceLoader- 支持 UTF-8,但您必须更改配置格式才能使用它
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
所以我们决定编写我们自己的 PropertySourceLoader 实现,它能够正确地从 UTF-8 文件加载属性。这个想法来自答案@BalusC - How to use UTF-8 in resource properties with ResourceBundle
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.factorieswith content:
然后我们创建了包含内容的文件resources/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,按以下顺序排列:
- UnicodePropertiesPropertySourceLoader
- PropertiesPropertySourceLoader
- YamlPropertySourceLoader
- UnicodePropertiesPropertySourceLoader
- PropertiesPropertySourceLoader
- YamlPropertySourceLoader
NOTES!
笔记!
- I'm not sure that it is proper usage of PropertyResourceBundle
- 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.
- 我不确定它是否正确使用了 PropertyResourceBundle
- 如果您制作专用库以在其他项目中重用它,我不确定 Spring Boot 中 PropertySourceLoaders 的顺序是否相同。
In our project this solution works fine.
在我们的项目中,这个解决方案工作正常。
UPDATE!
更新!
It's better to implement load method of UnicodePropertiesPropertySourceLoader without PropertyResourceBundle:
最好在没有 PropertyResourceBundle 的情况下实现 UnicodePropertiesPropertySourceLoader 的 load 方法:
@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;
}
回答by May12
Please, try to add PropertySource
annotation with encoding parameter into your Configuaration file:
请尝试将PropertySource
带有编码参数的注释添加到您的配置文件中:
@PropertySource(value = "classpath:application-${env}.properties", encoding = "UTF-8")
Hope it helps.
希望能帮助到你。
回答by Aleksey
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.
要为 application.properties(以及任何其他 Java 属性以及环境变量)中的文本设置 UTF-8 编码,请添加-Dfile.encoding=UTF-8
到 java 命令行 agrs。
回答by gischy
just converted the text with the special chars with https://native2ascii.net/
刚刚使用https://native2ascii.net/转换了带有特殊字符的文本