spring boot项目中,某配置文件有中文内容,结果读取出来是乱码。
首先,这个文件编码是UTF-8的,然后我的intellij idea默认编码也是utf-8的,但8来8去,它就是乱码啊。
当然啦,不写汉字,将中文内容改为unicode方式(即类似这种方式:\u672a\u89c9\u6c60\u5858\u6625\u8349\u68a6\uff0c\u9636\u524d\u68a7\u53f6\u5df2\u79cb\u58f0
)是可以的,不会出现乱码。但问题是,这样可读性太差,失去可配置的意义。
应该在读取的时候,显式指定编码。
怎么指定呢?
这样指定:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value = "classpath:webconfig.properties",encoding = "UTF-8")
@ConfigurationProperties("web")
public class WebConfig {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
web.properties
web.title=秋雨敲窗天渐凉
- 1