Obtain Sonarlint specification restrictions for files under the resources folder in the java project

Preface: The company requires developers to strictly follow the sonarlint specification in the process of writing code.

Download word documents according to business needs and use template downloads. Put the word template in the resources directory to download.

The project structure is as follows

insert image description here

Start thinking about getting the path with the following method

 String templateFile = this.getClass().getClassLoader().getResource("word/inspection.docx").getPath();

Sonarlint's specification prompt appeared when obtaining the path.

In a JEE context, using the standard getClassLoader() may not return the correct class loader. Instead, use currentThread.

insert image description here
Change it to the following, following the sonarlint specification:

String templateFile = Thread.currentThread().getContextClassLoader().getResource("word/inspection.docx").getPath();

The above two methods can successfully obtain the path, and the code of the second method is more standardized.

Guess you like

Origin blog.csdn.net/aq_money/article/details/128467052