springboot讀取自定義配置文件時出現(xiàn)亂碼解決方案
這是入門的第三天了,從簡單的hello spring開始,已經(jīng)慢慢接近web的樣子。接下來當(dāng)然是讀取簡單的對象屬性了。
于是按照網(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里面配置,那樣的話這個文件太臃腫了。
當(dāng)然了文件的編碼肯定是選的UTF-8不要懷疑,
然而遺憾的是,當(dāng)我在瀏覽器輸入http://localhost:9090/wow/book訪問時,竟然亂碼了?。?!哦shit
然后就是各種找解決方案,順便也了解到了原因,就是eclipse默認(rèn).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默認(rèn)指定所有文件編碼是UTF-8,這個文件已經(jīng)指定,并沒有作用。
方案三
編寫讀取properties文件的類來控制輸出流,特么的這個類在哪里調(diào)用?
方案四
嗯,eclipse需要一個讀取properties文件的插件,對的就是插件,下載一個插件據(jù)說就能UTF-8輸出了,然而我并不想因為一個文件就去下載一個插件。所以這種方案沒有試。
方案五
據(jù)說yml可以輸出中文不亂碼???那還不簡單,換個格式不就完了?
naive!
首先,將book.properties換成book.yml,各種鏈接給出的方案都是在默認(rèn)配置文件里寫簡直了。。。。。。
然后根據(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)文章
Spring Boot中配置文件application.properties使用
這篇文章主要介紹了Spring Boot中配置文件application.properties使用及spring boot讀取application.properties文件的方式,需要的朋友參考下吧2018-01-01SpringBoot項目中的favicon.ico圖標(biāo)無法顯示問題及解決
這篇文章主要介紹了SpringBoot項目中的favicon.ico圖標(biāo)無法顯示問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01通過反射注解批量插入數(shù)據(jù)到DB的實現(xiàn)方法
今天小編就為大家分享一篇關(guān)于通過反射注解批量插入數(shù)據(jù)到DB的實現(xiàn)方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03Java網(wǎng)絡(luò)編程基礎(chǔ)用法詳解
網(wǎng)絡(luò)編程是指編寫運行在多個設(shè)備(計算機)的程序,這些設(shè)備都通過網(wǎng)絡(luò)連接起來,本文將帶大家詳細(xì)了解Java的網(wǎng)絡(luò)編程,文中有相關(guān)的代碼示例,需要的朋友可以參考下2023-05-05Java中靜態(tài)類型檢查是如何進(jìn)行的實例思路詳解
這篇文章主要介紹了Java中靜態(tài)類型檢查是如何進(jìn)行的實例思路詳解的相關(guān)資料,需要的朋友可以參考下2016-05-05使用shardingsphere對SQLServer坑的解決
本文主要介紹了使用shardingsphere對SQLServer坑的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03基于SpringBoot集成測試遠(yuǎn)程連接Redis服務(wù)的教程詳解
這篇文章主要介紹了基于SpringBoot集成測試遠(yuǎn)程連接的Redis服務(wù)的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03