简体   繁体   English

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

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

  • PropertiesPropertySourceLoader - supports UTF-8 only when load from XML PropertiesPropertySourceLoader - 仅在从XML加载时支持UTF-8
  • YamlPropertySourceLoader - supports UTF-8, but you have to change configuration format to use it 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 它们列在文件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,顺序如下:

  • UnicodePropertiesPropertySourceLoader UnicodePropertiesPropertySourceLoader
  • PropertiesPropertySourceLoader PropertiesPropertySourceLoader
  • YamlPropertySourceLoader YamlPropertySourceLoader

NOTES! 笔记!

  1. I'm not sure that it is proper usage of PropertyResourceBundle 我不确定它是否正确使用了PropertyResourceBundle
  2. 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. 如果您创建一个专用库以在其他项目中重用它,我不确定Spring Boot中PropertySourceLoaders的顺序是否相同。

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.

相关问题 spring-boot application.properties中的环境变量错误 - Environment Variables in spring-boot application.properties error 春季启动application.properties问题-bug - spring-boot application.properties question -bug Spring-Boot&gt;使用JNDI设置application.properties - Spring-Boot > Setting application.properties using JNDI application.properties 中电子邮件数据中 Spring-Boot 中的环境变量 - environment variables in Spring-Boot in email data in application.properties 覆盖application.properties以在Spring-boot应用程序中进行集成测试 - Override application.properties for integration tests in spring-boot app 如何在 Spring-Boot 的生产过程中覆盖 application.properties? - How to override application.properties during production in Spring-Boot? spring-boot如何添加多个application.properties文件? - How to add multiple application.properties files in spring-boot? Spring-boot - 参数化测试 - 访问MethodSource中的Application.properties - Spring-boot - Parameterized Test - Access Application.properties in MethodSource 用application.properties覆盖默认的Spring-Boot application.properties - Override default Spring-Boot application.properties with application.properties Application.properties spring-boot 在 pom.xml 中产生错误。 application.properties 中的 mysql 连接存在问题 - Application.properties spring-boot produce an error in pom.xml. Problem with mysql connection in application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM