springboot讀取自定義配置文件時出現(xiàn)亂碼解決方案
這是入門的第三天了,從簡單的hello spring開始,已經(jīng)慢慢接近web的樣子。接下來當然是讀取簡單的對象屬性了。
于是按照網(wǎng)上各位大神教的,簡單寫了個對象book,如上一篇,其他配置不需要做任何改動。
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,那么知道了編碼的問題,按照以前的思路來解決亂碼就比較順暢了,
無非是優(yōu)先指定服務(wù)器編碼,這就是方案一,要么指定瀏覽器編碼,然而因為不是jsp訪問所以這個行不通,要么文件編碼指定UTF-8,然而無效。
網(wǎng)上提供的解決方案也不外乎這幾種
方案一
在application里面指定tomcat的編碼:
#設(shè)置中文字符的顯示方式 #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,這個文件已經(jīng)指定,并沒有作用。
方案三
編寫讀取properties文件的類來控制輸出流,特么的這個類在哪里調(diào)用?
方案四
嗯,eclipse需要一個讀取properties文件的插件,對的就是插件,下載一個插件據(jù)說就能UTF-8輸出了,然而我并不想因為一個文件就去下載一個插件。所以這種方案沒有試。
方案五
據(jù)說yml可以輸出中文不亂碼???那還不簡單,換個格式不就完了?
naive!
首先,將book.properties換成book.yml,各種鏈接給出的方案都是在默認配置文件里寫簡直了。。。。。。
然后根據(jù)指點,使用@value將屬性注入,各大網(wǎng)站給出的注入位置幾乎都在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并沒有什么特權(quán)。有人說yml也是解析成properties文件運行的,嗯,這個解釋我服。
難道真的只能下載插件了?NONONO不要輕言放棄。更換關(guān)鍵字繼續(xù)搜索,終于找到了一篇:
終極大法來了:
方案六
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上面。其次,在讀取數(shù)據(jù)源的@PropertySource里面指定文件編碼方式。
這樣訪問就能正常顯示中文了。
同理,properties文件也可以這樣做,只要@PropertySource(value = "classpath:book.properties", ignoreResourceNotFound = true,encoding = "UTF-8" )就行了,根本不需要什么插件!
另,這個配置文件的路徑也可以自定義而不需要在@PropertySource(value = "classpath:“)里面給出。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Elasticsearch算分優(yōu)化方案之rescore_query示例詳解
這篇文章主要為大家介紹了Elasticsearch算分優(yōu)化方案之rescore_query示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
Mybatis下動態(tài)sql中##和$$的區(qū)別講解
今天小編就為大家分享一篇關(guān)于Mybatis下動態(tài)sql中##和$$的區(qū)別講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
spring task @Scheduled注解各參數(shù)的用法
這篇文章主要介紹了spring task @Scheduled注解各參數(shù)的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
Java數(shù)據(jù)結(jié)構(gòu)篇之實現(xiàn)二叉搜索樹的核心方法
二叉搜索樹是一種常用的數(shù)據(jù)結(jié)構(gòu),它是一棵二叉樹,且每個節(jié)點的值都大于其左子樹中任何節(jié)點的值,而小于其右子樹中任何節(jié)點的值,這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)結(jié)構(gòu)篇之實現(xiàn)二叉搜索樹的核心方法,需要的朋友可以參考下2023-12-12
Spring?Boot集成validation實現(xiàn)參數(shù)校驗功能
Bean?Validation?是一個運行時的數(shù)據(jù)驗證框架,在驗證之后驗證的錯誤信息會被馬上返回,這篇文章主要介紹了Spring?Boot集成validation實現(xiàn)參數(shù)校驗功能,需要的朋友可以參考下2024-05-05
深入分析Comparable與Comparator及Clonable三個Java接口
接口不是類,而是對類的一組需求描述,這些類要遵從接口描述的統(tǒng)一格式進行定義,這篇文章主要為大家詳細介紹了Java的Comparable,Comparator和Cloneable的接口,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-05-05

