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

Pat*_*ick 22 java encoding spring utf-8 spring-boot

在我的application.properties添加一些自定义属性.

custom.mail.property.subject-message=This is a ä ö ü ß problem
Run Code Online (Sandbox Code Playgroud)

在这个类中,我有自定义属性的表示.

@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;
    }
Run Code Online (Sandbox Code Playgroud)

在这里我用我的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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

调试时我可以看到我的this.getSubjectMessage()变量里面有这个值:This is a ä ö ü à problem.所以在发送邮件之前我已经有了UTF-8编码问题.

我已经检查了application.properties文件的编码及其UTF-8.

我的IDE(STS/Eclipse)和项目属性也在UTF-8上设置.

如何在文件中为自定义属性的文本设置UTF-8编码application.properties

Hen*_*nry 19

正如评论中已经提到的那样.properties文件应该在ISO 8859-1中编码.可以使用unicode转义来指定其他字符.还有一个工具可用于转换.例如,这可以在自动构建中使用,这样您仍然可以在源中使用您喜欢的编码.

  • 太棒了,谢谢,但这个答案会更好,举例说明编码字符在 application.properties 中的样子:custom.mail.property.subject-message=这是一个 \u00E4 \u00F6 \u00FC \u00DF 问题 (3认同)
  • 在这里找到在线转换工具:https://native2ascii.net/ (2认同)

May*_*y12 15

请尝试将PropertySource带编码参数的注释添加到Configuaration文件中:

@PropertySource(value = "classpath:application-${env}.properties", encoding = "UTF-8")
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

  • @Erk 好问题。spring boot 文档的黑魔法炼金术说如下:将其添加到`@Configuration`类:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/上下文/注释/PropertySource.html (2认同)

小智 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;
}

}
Run Code Online (Sandbox Code Playgroud)

然后我们用内容创建了文件资源/ META-INF/spring.factories:

# Custom PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
your.own.package.UnicodePropertiesPropertySourceLoader
Run Code Online (Sandbox Code Playgroud)

现在我们的应用程序中有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;
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*sey 5

要为 application.properties(以及任何其他 Java 属性以及环境变量)中的文本设置 UTF-8 编码,请添加-Dfile.encoding=UTF-8到 java 命令行 agrs。

  • 作为一个副作用,在 Docker 容器中使用 ```-Dfile.encoding=UTF-8``` 运行的 Java 应用程序会生成 unicode 格式的可读输出,而不是问号。因此,这个神奇的标志可以解决这两个问题,而无需任何代码行。有一个相关主题解释了一些细节/sf/ask/25338281/ (2认同)

gis*_*chy 5

刚刚使用https://native2ascii.net/转换了带有特殊字符的文本