(Solved) When SpringBoot reads the yml/yaml/properties configuration file, use the annotation @Value to get the Chinese garbled problem

Problem phenomenon:

Today, in the project, it is necessary to retrieve the specified field in the corresponding table according to a certain Chinese value in the database, so as to obtain the relevant information of the row record., because it is not sure whether the Chinese value remains unchanged, in order to improve the code efficiency, I put the Chinese The value is obtained through the configuration file definition:

Instead of writing dead in the code, then use the @Vaule annotation to get this attribute value:

As a result, when debugging the code, it was found to be garbled:


problem analysis:

By consulting the relevant information, it is found that in the underlying logic of Sprigboot , when using the @Vaule annotation to obtain the data value of the configuration file, the ISO-8859-1 encoding  format is used  , so the Chinese cannot be successfully recognized;

There are some methods on the Internet that I do not recommend , such as:

1. It can be configured by adding springboot

2. Set the Transparent native-to-ascii conversion of File Encodings to true;

Here I introduce a quick and accurate method to get the Chinese value:

When the Chinese value needs to be used, the variable value is transcoded in the encoding format and converted to UTF-8, and the Chinese can be recognized.


Solution:

Modify the encoding format of the mediumVoltageCircuitTableName variable to UTF-8:

		String devSpecificationName = new String(mediumVoltageCircuitTableName.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

Debugging results:

Guess you like

Origin blog.csdn.net/weixin_42585386/article/details/111193504