(C)spring boot读取自定义配置文件时乱码解决办法
这是入门的第三天了,从简单的hello spring开始,已经慢慢接近web的样子。接下来当然是读取简单的对象属性了。
于是按照网上各位大神教的,简单写了个对象book,如上一篇(B),其他配置不需要做任何改动。
package com.example.bean; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "book")
@PropertySource("classpath:book.properties") public class Book { private String name; private String author; private String price; public Book () {}; public Book(String name,String author,String price) {
this.name = name;
this.author = author;
this.price = price;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} public String getPrice() {
return price;
} public void setPrice(String price) {
this.price = price;
}; }
访问控制器controller如下:
@Controller
public class BookController { @Autowired
private Book book; @RequestMapping("/book")
@ResponseBody
public String readBook() {
return "emmmm..... The BookName is "
+book.getName()
+";and Book Author is "
+book.getAuthor()
+";and Book price is "
+book.getPrice(); }
}
book的属性值在配置文件里面给出,我用了自定义配置文件,没有在application.properties里面配置,那样的话这个文件太臃肿了。
当然了文件的编码肯定是选的UTF-8不要怀疑,
然而遗憾的是,当我在浏览器输入http://localhost:9090/wow/book访问时,竟然乱码了!!!哦shit
然后就是各种找解决方案,顺便也了解到了原因,就是eclipse默认.properties文件的编码是ISO-8859-1,那么知道了编码的问题,按照以前的思路来解决乱码就比较顺畅了,
无非是优先指定服务器编码,这就是方案一,要么指定浏览器编码,然而因为不是jsp访问所以这个行不通,要么文件编码指定UTF-8,然而无效。
网上提供的解决方案也不外乎这几种:
方案一:在application里面指定tomcat的编码:
#设置中文字符的显示方式 #server.tomcat.uri-encoding=UTF-8
#spring.http.encoding.charset=UTF-8
#spring.http.encoding.enabled=true
#spring.http.encoding.force=true
#spring.messages.encoding=UTF-8
并无卵用!第一行直接就报错了!我的JDK1.8,spring boot2.0,可能是版本问题,反正就是报错不能用。
方案二:各种通过文件编码指定的,不可用。我eclipse默认指定所有文件编码是UTF-8,这个文件已经指定,并没有作用。
方案三:编写读取properties文件的类来控制输出流,特么的这个类在哪里调用?
方案四:嗯,eclipse需要一个读取properties文件的插件,对的就是插件,下载一个插件据说就能UTF-8输出了,然而我并不想因为一个文件就去下载一个插件。所以这种方案没有试。
方案五:据说yml可以输出中文不乱码???那还不简单,换个格式不就完了?
naive!
首先,将book.properties换成book.yml,各种链接给出的方案都是在默认配置文件里写简直了。。。。。。
然后根据指点,使用@value将属性注入,各大网站给出的注入位置几乎都在get set 方法上面,然而,
找不到文件啊衰。。。。依然乱码啊(′д` )…彡…彡
乱码:
package com.example.bean; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(value = "book")
@PropertySource("classpath:book.yml")
public class BookYml {//仍然是乱码 private String name; private String author; private String price; public BookYml () {}; public BookYml(String name,String author,String price) {
this.name = name;
this.author = author;
this.price = price;
} @Value("${book.name}")
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
@Value("${book.author}")
public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
}
@Value("${book.price}")
public String getPrice() {
return price;
} public void setPrice(String price) {
this.price = price;
}; }
嗯,既然仍然是乱码,说明yml并没有什么特权。有人说yml也是解析成properties文件运行的,嗯,这个解释我服。
难道真的只能下载插件了?NONONO不要轻言放弃。更换关键字继续搜索,终于找到了一篇:
终极大法来了:
方案六:
package com.example.bean; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Component
@PropertySource(value = "classpath:book.yml", ignoreResourceNotFound = true,encoding = "UTF-8" )
@ConfigurationProperties(prefix = "book")public class Book { @Value("${name}")
private String name;
@Value("${author}")
private String author;
@Value("${price}")
private String price; public Book () {}; public Book(String name,String author,String price) {
this.name = name;
this.author = author;
this.price = price;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} public String getPrice() {
return price;
} public void setPrice(String price) {
this.price = price;
}; }
完美!
首先要在属性声明上引入注解@value,并不是在get set上面。其次,在读取数据源的@PropertySource里面指定文件编码方式。
这样访问就能正常显示中文了。
同理,properties文件也可以这样做,只要@PropertySource(value = "classpath:book.properties", ignoreResourceNotFound = true,encoding = "UTF-8" )就行了,根本不需要什么插件!
另,这个配置文件的路径也可以自定义而不需要在@PropertySource(value = "classpath:“)里面给出。
感谢 https://ask.csdn.net/questions/681857?sort=votes_count
(C)spring boot读取自定义配置文件时乱码解决办法的更多相关文章
- spring boot读取自定义配置文件时乱码解决办法
@PropertySource(value = "classpath:book.yml", ignoreResourceNotFound = true,encoding = &qu ...
- Spring Boot读取自定义外部属性
测试的环境:Spring Boot2 + Maven +lombok 准备需要用到的基础类: public class People { private String name; private St ...
- spring boot jpa 使用update 报错解决办法
在spring boot jpa 中自定义sql,执行update操作报错解决办法: 在@Query(...)上添加 @Modifying@Transactional注解
- spring boot中log4j冲突问题和解决办法
Spring Boot中自带了log4j日志管理.写法应该是: private static final Logger logger = Logger.getLogger(XXX.class); 而不 ...
- Spring boot读取application.properties中文乱码
解决方案 在IDEA环境下: File -> Settings -> Editor -> File Encodings 将Properties Files (*.properties ...
- spring mvc 的@PathVariable对应中文乱码解决办法
参考:https://www.aliyun.com/jiaocheng/800577.html
- spring boot 项目从配置文件中读取maven 的pom.xml 文件标签的内容。
需求: 将pom.xml 文件中的版本号读取到配置文件并打印到日志中. 第一步: 在pom.xml 中添加以下标签. 第二步: 将version 标签的值读取到配置文件中 这里使用 @@ 而不是 ...
- Spring Boot的properties配置文件读取
我在自己写点东西玩的时候需要读配置文件,又不想引包,于是打算扣点Spring Boot读取配置文件的代码出来,当然只是读配置文件没必要这么麻烦,不过反正闲着也是闲着,扣着玩了.具体启动过程以前的博客写 ...
- Springboot读取自定义配置文件的几种方法
一.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. ...
随机推荐
- HTML5 data-* 自定义属性操作及其注意点
在HTML5中添加了data-*的方式来自定义属性,所谓data-*实际上上就是data-前缀加上自定义的属性名,命名可以用驼峰命名方式,但取值是必需全部使用小写(后面会说),使用这样的结构可以进行数 ...
- centos6.5_64bit_tomcat日志合并在一个.log下
问题 tomcat每次启动时,自动在logs目录下生产以下日志文件,且每天都会生成对应日期的一个文件,造成日志文件众多: 目的 Tomcat以上日志都输出到同一个文件中. 修 ...
- Spark python集成
Spark python集成 1.介绍 Spark支持python语言,对于大量的SQL类型的操作,不需要编译,可以直接提交python文件给spark来运行,因此非常简单方便,但是性能要比scala ...
- spring-mabatis整合的配置文件
1.spring.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- 使用Python命令创建jenkins的job
目的:通过调用jenkins的命令,动态创建jenkins的job 如何使用,使用Python的脚本,更多API可以进入到官网去查看,http://jenkinsapi.readthedocs.io/ ...
- jemter实战
业务流梳理,需要实现的内容,含jdbc和正则表达式,和取值 1. 登录用户 2. 获取一个未分配的订单,判断是否是未分配 3. 获取配货单号 4. 分配配货单 5. 查询已分配的配货单 6. 查询包裹 ...
- 【CCPC-Wannafly Winter Camp Day4 (Div1) C】最小边覆盖(简单题)
点此看题面 大致题意: 给你一个边集的子集,问你这可不可能是这张图的最小边覆盖. 大致思路 考虑到,如果一条边连接的两个点度数都大于等于\(2\),则这条边完全可以删去. 因此,我们只要判断是否存在这 ...
- data-ng-repeat 指令
data-ng-repeat指令对于集合中的每一项会克隆一次HTML元素.
- C# while语句
一.C# while语句 while语句是用于重复执行程序代码的语句. 语法格式如下: while(boolean-expression){ embedded-statement} 当boole ...
- 其它内置函数(zip等)
python内置函数 截止到python版本3.6.2,python一共为我们提供了68个内置函数.它们就是python提供给你直接可以拿来使用的所有函数 Built-in Functio ...