解决SpringCloudConfig中文乱码问题
问题来自于配置服务端使用的编码格式为ISO-8859-1导致
这里处理后的结果
添加两个类CustomizedOriginTrackedPropertiesLoader和CustomizedPropertiesPropertySourceLoader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | package com.config; import org.springframework.boot.origin.Origin; import org.springframework.boot.origin.OriginTrackedValue; import org.springframework.boot.origin.TextResourceOrigin; import org.springframework.core.io.Resource; import org.springframework.util.Assert; import java.io.Closeable; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.nio.charset.StandardCharsets; import java.util.LinkedHashMap; import java.util.Map; /** * @Description: * @Author: Yourheart * @Create: 2022/12/10 20:31 */ public class CustomizedOriginTrackedPropertiesLoader { private final Resource resource; CustomizedOriginTrackedPropertiesLoader(Resource resource) { Assert.notNull(resource, "Resource must not be null" ); this .resource = resource; } Map<String, OriginTrackedValue> load() throws IOException { return this .load( true ); } Map<String, OriginTrackedValue> load( boolean expandLists) throws IOException { CustomizedOriginTrackedPropertiesLoader.CharacterReader reader = new CustomizedOriginTrackedPropertiesLoader.CharacterReader( this .resource); Throwable var3 = null ; try { Map<String, OriginTrackedValue> result = new LinkedHashMap(); StringBuilder buffer = new StringBuilder(); while (reader.read()) { String key = this .loadKey(buffer, reader).trim(); if (expandLists && key.endsWith( "[]" )) { key = key.substring( 0 , key.length() - 2 ); int var19 = 0 ; while ( true ) { OriginTrackedValue value = this .loadValue(buffer, reader, true ); this .put(result, key + "[" + var19++ + "]" , value); if (!reader.isEndOfLine()) { reader.read(); } if (reader.isEndOfLine()) { break ; } } } else { OriginTrackedValue value = this .loadValue(buffer, reader, false ); this .put(result, key, value); } } LinkedHashMap var18 = (LinkedHashMap) result; return var18; } catch (Throwable var16) { var3 = var16; throw var16; } finally { if (reader != null ) { if (var3 != null ) { try { reader.close(); } catch (Throwable var15) { var3.addSuppressed(var15); } } else { reader.close(); } } } } private void put(Map<String, OriginTrackedValue> result, String key, OriginTrackedValue value) { if (!key.isEmpty()) { result.put(key, value); } } private String loadKey(StringBuilder buffer, CustomizedOriginTrackedPropertiesLoader.CharacterReader reader) throws IOException { buffer.setLength( 0 ); boolean previousWhitespace = false ; while (!reader.isEndOfLine()) { if (reader.isPropertyDelimiter()) { reader.read(); return buffer.toString(); } if (!reader.isWhiteSpace() && previousWhitespace) { return buffer.toString(); } previousWhitespace = reader.isWhiteSpace(); buffer.append(reader.getCharacter()); reader.read(); } return buffer.toString(); } private OriginTrackedValue loadValue(StringBuilder buffer, CustomizedOriginTrackedPropertiesLoader.CharacterReader reader, boolean splitLists) throws IOException { buffer.setLength( 0 ); while (reader.isWhiteSpace() && !reader.isEndOfLine()) { reader.read(); } TextResourceOrigin.Location location = reader.getLocation(); while (!reader.isEndOfLine() && (!splitLists || !reader.isListDelimiter())) { buffer.append(reader.getCharacter()); reader.read(); } Origin origin = new TextResourceOrigin( this .resource, location); return OriginTrackedValue.of(buffer.toString(), origin); } private static class CharacterReader implements Closeable { private static final String[] ESCAPES = new String[]{ "trnf" , "\t\r\n\f" }; private final LineNumberReader reader; private int columnNumber = - 1 ; private boolean escaped; private int character; CharacterReader(Resource resource) throws IOException { this .reader = new LineNumberReader( new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)); } public void close() throws IOException { this .reader.close(); } boolean read() throws IOException { return this .read( false ); } boolean read( boolean wrappedLine) throws IOException { this .escaped = false ; this .character = this .reader.read(); ++ this .columnNumber; if ( this .columnNumber == 0 ) { this .skipLeadingWhitespace(); if (!wrappedLine) { this .skipComment(); } } if ( this .character == 92 ) { this .escaped = true ; this .readEscaped(); } else if ( this .character == 10 ) { this .columnNumber = - 1 ; } return ! this .isEndOfFile(); } private void skipLeadingWhitespace() throws IOException { while ( this .isWhiteSpace()) { this .character = this .reader.read(); ++ this .columnNumber; } } private void skipComment() throws IOException { if ( this .character == 35 || this .character == 33 ) { while ( true ) { if ( this .character == 10 || this .character == - 1 ) { this .columnNumber = - 1 ; this .read(); break ; } this .character = this .reader.read(); } } } private void readEscaped() throws IOException { this .character = this .reader.read(); int escapeIndex = ESCAPES[ 0 ].indexOf( this .character); if (escapeIndex != - 1 ) { this .character = ESCAPES[ 1 ].charAt(escapeIndex); } else if ( this .character == 10 ) { this .columnNumber = - 1 ; this .read( true ); } else if ( this .character == 117 ) { this .readUnicode(); } } private void readUnicode() throws IOException { this .character = 0 ; for ( int i = 0 ; i < 4 ; ++i) { int digit = this .reader.read(); if (digit >= 48 && digit <= 57 ) { this .character = ( this .character << 4 ) + digit - 48 ; } else if (digit >= 97 && digit <= 102 ) { this .character = ( this .character << 4 ) + digit - 97 + 10 ; } else { if (digit < 65 || digit > 70 ) { throw new IllegalStateException( "Malformed \\uxxxx encoding." ); } this .character = ( this .character << 4 ) + digit - 65 + 10 ; } } } boolean isWhiteSpace() { return ! this .escaped && ( this .character == 32 || this .character == 9 || this .character == 12 ); } boolean isEndOfFile() { return this .character == - 1 ; } boolean isEndOfLine() { return this .character == - 1 || ! this .escaped && this .character == 10 ; } boolean isListDelimiter() { return ! this .escaped && this .character == 44 ; } boolean isPropertyDelimiter() { return ! this .escaped && ( this .character == 61 || this .character == 58 ); } char getCharacter() { return ( char ) this .character; } TextResourceOrigin.Location getLocation() { return new TextResourceOrigin.Location( this .reader.getLineNumber(), this .columnNumber); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.config; import org.springframework.boot.env.OriginTrackedMapPropertySource; import org.springframework.boot.env.PropertySourceLoader; import org.springframework.core.env.PropertySource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; import java.io.IOException; import java.util.Collections; import java.util.List; import java.util.Map; /** * @Description: * @Author: Yourheart * @Create: 2022/12/10 21:10 */ public class CustomizedPropertiesPropertySourceLoader implements PropertySourceLoader { @Override public String[] getFileExtensions() { return new String[]{ "properties" , "xml" }; } @Override public List<PropertySource<?>> load(String name, Resource resource) throws IOException { Map<String, ?> properties = this .loadProperties(resource); return properties.isEmpty() ? Collections.emptyList() : Collections.singletonList( new OriginTrackedMapPropertySource(name, Collections.unmodifiableMap(properties), true )); } private Map<String, ?> loadProperties(Resource resource) throws IOException { String filename = resource.getFilename(); return (Map)(filename != null && filename.endsWith( ".xml" ) ? PropertiesLoaderUtils.loadProperties(resource) : ( new CustomizedOriginTrackedPropertiesLoader(resource)).load()); } } |
为了让修改的配置生效
1 2 | org.springframework.boot.env.PropertySourceLoader=\ com.config.CustomizedPropertiesPropertySourceLoader |
标签:
SpringCloudConfig
, 问题
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异