繁体   English   中英

Spring-Boot中的application.properties属性的UTF-8编码

[英]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,用于在应用程序中加载属性:

  • PropertiesPropertySourceLoader - 仅在从XML加载时支持UTF-8
  • YamlPropertySourceLoader - 支持UTF-8,但您必须更改配置格式才能使用它

它们列在文件https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/resources/META-INF/spring.factories中

所以我们决定编写自己的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,顺序如下:

  • UnicodePropertiesPropertySourceLoader
  • PropertiesPropertySourceLoader
  • YamlPropertySourceLoader

笔记!

  1. 我不确定它是否正确使用了PropertyResourceBundle
  2. 如果您创建一个专用库以在其他项目中重用它,我不确定Spring Boot中PropertySourceLoaders的顺序是否相同。

在我们的项目中,此解决方案正常。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM