溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么解決springboot讀取自定義配置文件時出現(xiàn)亂碼問題

發(fā)布時間:2021-11-20 09:32:51 來源:億速云 閱讀:389 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“怎么解決springboot讀取自定義配置文件時出現(xiàn)亂碼問題”,在日常操作中,相信很多人在怎么解決springboot讀取自定義配置文件時出現(xiàn)亂碼問題問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么解決springboot讀取自定義配置文件時出現(xiàn)亂碼問題”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(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
@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里面配置,那樣的話這個文件太臃腫了。

怎么解決springboot讀取自定義配置文件時出現(xiàn)亂碼問題

當(dāng)然了文件的編碼肯定是選的UTF-8不要懷疑,

怎么解決springboot讀取自定義配置文件時出現(xiàn)亂碼問題

然而遺憾的是,當(dāng)我在瀏覽器輸入http://localhost:9090/wow/book訪問時,竟然亂碼了?。?!哦shit

怎么解決springboot讀取自定義配置文件時出現(xiàn)亂碼問題

然后就是各種找解決方案,順便也了解到了原因,就是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;
    };
}

怎么解決springboot讀取自定義配置文件時出現(xiàn)亂碼問題

到此,關(guān)于“怎么解決springboot讀取自定義配置文件時出現(xiàn)亂碼問題”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI