Spring-Boot

时间:2016-05-25 12:19:38

标签: java spring encoding utf-8 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编码?

5 个答案:

答案 0 :(得分:17)

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

答案 1 :(得分:12)

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

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

希望它有所帮助。

答案 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 - How to use UTF-8 in resource properties with ResourceBundle

我们的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;
}

}

然后我们用内容创建了文件 resources / 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的顺序是否相同。
  3. 在我们的项目中,此解决方案正常运行。

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

答案 3 :(得分:2)

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

答案 4 :(得分:0)

只需将带有特殊字符的文本转换为https://native2ascii.net/