![](/img/trans.png)
[英]Environment Variables in spring-boot application.properties error
[英]UTF-8 encoding of application.properties attributes in Spring-Boot
在我的application.properties
我添加了一些自定义属性。
custom.mail.property.subject-message=This is a ä ö ü ß problem
在这个类中,我有自定义属性的表示。
@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编码问题。
我已经检查了application.properties
文件及其UTF-8的编码。
我的IDE(STS / Eclipse)和项目属性也在UTF-8上设置。
如何在application.properties
文件中为自定义属性的文本设置UTF-8编码?
正如评论中已经提到的那样.properties文件应该在ISO 8859-1中编码。 可以使用unicode转义来指定其他字符。 还有一个工具可用于转换。 例如,这可以在自动构建中使用,这样您仍然可以在源中使用您喜欢的编码。
请尝试将带有编码参数的PropertySource
注释添加到Configuaration文件中:
@PropertySource(value = "classpath:application-${env}.properties", encoding = "UTF-8")
希望能帮助到你。
我遇到了同样的问题。 在Spring Boot中有2个PropertySourceLoader,用于在应用程序中加载属性:
所以我们决定编写自己的PropertySourceLoader实现,它可以正确地从UTF-8文件加载属性。 这个想法是从@BalusC回答 - 如何在ResourceBundle的资源属性中使用UTF-8
我们的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;
}
}
然后我们用内容创建了文件资源/ META-INF / spring.factories :
# Custom PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
your.own.package.UnicodePropertiesPropertySourceLoader
现在我们的应用程序中有3个PropertySourceLoader,顺序如下:
笔记!
在我们的项目中,此解决方案正常。
UPDATE!
最好在没有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.