In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is "how to solve the problem of garbled codes in Springboot-application.properties". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to solve the problem of garbled codes in Springboot-application.properties".
Springboot-application.properties Chinese garbled code
Springboot-application.properties coding problem set application.properties to utf-8 read configuration Chinese result printing analysis
Set application.properties to utf-8
UTF-8, so that viewing configuration files in windows and linux servers can display Chinese normally. Otherwise, Chinese may not be displayed properly.
The configuration in application.properties is as follows:
Demo.to-who= Zhang San reads the configured Chinese @ RestControllerpublic class TestController {@ Value ("${demo.to-who}") private String toWho; @ RequestMapping ("/ test2") public Object test2 () throws UnsupportedEncodingException {System.out.println (toWho); System.out.println (new String (toWho.getBytes ("iso8859-1") System.out.println (new String (toWho.getBytes ("iso8859-1"), "utf-8"); return null;}} result printing
You need to change the data encoding from iso8859-1 to utf-8 before it can be used properly.
One thousand per thousand.
Zhang San
Zhang San
Analysis.
In the source code
Private static class CharacterReader implements Closeable {/ / other codes omit CharacterReader (Resource resource) throws IOException {this.reader = new LineNumberReader (new InputStreamReader (resource.getInputStream (), StandardCharsets.ISO_8859_1));} / / other codes are omitted}
In other words, no matter which encoding format the application.properties file is set to, it will eventually be loaded in the encoding format of ISO-8859-1.
Yml/yaml is loaded with UTF-8 by default.
Springboot configuration file application.properties supports Chinese version notes
This article is not entirely based on springboot-2.4.5, each version needs to rewrite the logic of the class is different, the code in this article can only be imitated, can not be copied
Why is the Chinese PropertySourceLoader interface not supported?
First take a look at the interface org.springframework.boot.env.PropertySourceLoader that reads the configuration file.
='0' & & digit > loadProperties (Resource resource) throws IOException {String filename = resource.getFilename (); List result = new ArrayList (); if (filename! = null & & filename.endsWith (XML_FILE_EXTENSION)) {result.add ((Map) PropertiesLoaderUtils.loadProperties (resource)) } else {List documents = new MyOriginTrackedPropertiesLoader (resource). Load (); documents.forEach ((document)-> result.add (document.asMap ());} return result;}} 3. Create a spring.factories file
Create the / META-INF/spring.factories file under the resources resource directory
The contents of the file are
Org.springframework.boot.env.PropertySourceLoader=\ com.xxx.config.MyPropertiesPropertySourceLoader test
Create an application.properties file
Abc= Chinese test
Create the service class and inject the abc variable using @ Value
@ Value ("${abc}") private String abc
Break points in the load method of the com.xxx.config.MyPropertiesPropertySourceLoader class, the org.springframework.boot.env.PropertiesPropertySourceLoader class, and the org.springframework.boot.env.YamlPropertySourceLoader class
Running the project as debug, you can see that only the com.xxx.config.MyPropertiesPropertySourceLoader class and the org.springframework.boot.env.YamlPropertySourceLoader class are loaded
Send a request to view the value of the abc variable: Chinese test, no garbled code
At this point, I believe you have a deeper understanding of "how to solve the problem of garbled codes in Springboot-application.properties". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.