广告
返回顶部
首页 > 资讯 > 精选 >怎么解决springboot读取自定义配置文件时出现乱码问题
  • 634
分享到

怎么解决springboot读取自定义配置文件时出现乱码问题

2023-06-25 16:06:08 634人浏览 八月长安
摘要

这篇文章主要介绍“怎么解决SpringBoot读取自定义配置文件时出现乱码问题”,在日常操作中,相信很多人在怎么解决springboot读取自定义配置文件时出现乱码问题问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家

这篇文章主要介绍“怎么解决SpringBoot读取自定义配置文件时出现乱码问题”,在日常操作中,相信很多人在怎么解决springboot读取自定义配置文件时出现乱码问题问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么解决springboot读取自定义配置文件时出现乱码问题”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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如下:

@Controllerpublic 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读取自定义配置文件时出现乱码问题

当然了文件的编码肯定是选的UTF-8不要怀疑,

怎么解决springboot读取自定义配置文件时出现乱码问题

然而遗憾的是,当我在浏览器输入Http://localhost:9090/wow/book访问时,竟然乱码了!!!哦shit

怎么解决springboot读取自定义配置文件时出现乱码问题

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

怎么解决springboot读取自定义配置文件时出现乱码问题

到此,关于“怎么解决springboot读取自定义配置文件时出现乱码问题”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

--结束END--

本文标题: 怎么解决springboot读取自定义配置文件时出现乱码问题

本文链接: https://www.lsjlt.com/news/306017.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么解决springboot读取自定义配置文件时出现乱码问题
    这篇文章主要介绍“怎么解决springboot读取自定义配置文件时出现乱码问题”,在日常操作中,相信很多人在怎么解决springboot读取自定义配置文件时出现乱码问题问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家...
    99+
    2023-06-25
  • springboot读取自定义配置文件时出现乱码解决方案
    目录网上提供的解决方案也不外乎这几种方案一方案二方案三方案四方案五方案六这是入门的第三天了,从简单的hello spring开始,已经慢慢接近web的样子。接下来当然是读取简单的对象...
    99+
    2022-11-12
  • qt读取文件出现中文乱码怎么解决
    在Qt中读取文件时出现中文乱码的问题,可能是因为编码格式的不匹配。可以尝试以下方法解决: 使用QTextCodec设置正确的编码...
    99+
    2023-10-23
    qt
  • Python读取文件内容出现中文乱码怎么解决
    在Python中,如果你读取文件内容出现中文乱码,可以尝试以下方法解决:1. 指定文件编码:在使用`open()`函数读取文件时,可...
    99+
    2023-10-12
    Python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作