springboot读取自定义配置文件时出现乱码解决方案
作者:昨日的世界 发布时间:2022-01-29 11:09:40
这是入门的第三天了,从简单的hello spring开始,已经慢慢接近web的样子。接下来当然是读取简单的对象属性了。
于是按照网上各位大神教的,简单写了个对象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,那么知道了编码的问题,按照以前的思路来解决乱码就比较顺畅了,
无非是优先指定服务器编码,这就是方案一,要么指定浏览器编码,然而因为不是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://www.cnblogs.com/tulpen/p/9803116.html


猜你喜欢
- 在实际项目开发中,业务逻辑层的处理速度往往很快,特别是在开发Socket通信服务的时候,网络传输很快,但是一旦加上数据库操作,性能一落千丈,
- ////////////////////////////
- 在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileupload组件
- 本文实例讲述了C#实现异步GET的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.Coll
- 最近有一个java实验,要求用java使用数据库,于是本人新手小白,在idea上卡了好半天希望看到这个博客的人能解决问题,跳过一些坑首先,我
- JavaFXJavaFX 是一个开源的下一代客户端应用平台,适用于基于Java构建的桌面、移动端和嵌入式系统。 它是许多个人和公司的共同努力
- 一、Spring Boot Admin 的概念 Spring Boot Admin是一个
- 本文实例为大家分享了java实现马踏棋盘的具体代码,供大家参考,具体内容如下马踏棋盘算法介绍8X8棋盘,马走日字,要求每个方格只进入一次,走
- 事件(event),这个词儿对于初学者来说,往往总是显得有些神秘,不易弄懂。而这些东西却往往又是编程中常用且非常重要的东西。大家都知道win
- 自己定义的栈的接口,完全是按照栈的常用方法以及命名方式实现: 注意以下类,接口都是在一个命名空间下栈的接口:包括了常用的方法namespac
- 在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Thre
- Springboot添加server.servlet.context-pathserver.servlet.context-path配置的作
- 本文实例为大家分享了SSM实现学生管理系统的具体代码,供大家参考,具体内容如下概述基于Spring + Spring MVC 的学生管理系统
- Fragment是Android honeycomb 3.0开始新增的概念,Fragment名为碎片不过却和Activity十
- 本文实例分析了Android编程之json解析的方法。分享给大家供大家参考,具体如下:JSON的定义:一种轻量级的数据交换格式,具有良好的可
- 背景为了了解Seata AT模式的原理,我通过源码解读的方式画出了Seata AT模式启动的图示:如果是基于Springboot项目的话,项
- 前言我们有时候在开发中,遇到这样的问题,就是我们需要小程序授权登录我们自己的后台,通过小程序的信息换取我们自己后台的token,实现账号密码
- TextView文本大小自动适配与TextView边距的去除标题太难取了,其实本文主要就是讲如何控制文本大小,让其自动适配宽度,其次我们还需
- 一.话题引入在做项目过程中,我们一般都是最先编写登录注册功能,登录功能最重要的是登录成功后,系统还会保存该登录用户信息,这种保存用户信息的逻
- 本文实例为大家分享了android实现文件读写功能的具体代码,供大家参考,具体内容如下读取:public static String _ge