這是入門的第三天了,從簡單的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