SoFunction
Updated on 2025-03-08

How to read mock data in SpringBoot (efficient debugging interface)

Written in the previous chapter: Therefore, without accumulating small steps, you cannot reach a thousand miles; without accumulating small streams, you cannot form a river and sea.

Personal insights:

Recently, there are many demands for digital warehouses, and there are more large-structured jsons or strings. In order to save time, in the past, the local mock data was directly read and debugged from the database or the third-party SDK. It's really efficient, it can be self-tested by starting the project, unit tests or main functions. Record this experience and share it with everyone, and leave it for yourself to spare

Read resource resource file MOCK data in SpringBoot

Debugging interface

In Spring Boot projects, static resources or configuration files are usually placed insrc/main/resourcesIn the directory. Here is how to read and store it inresourcesSteps and code examples for text files in a directory:

File path

Put the file insrc/main/resourcesIn the directory, for example, the file name ismock_data.txt

Code Example

Can be used via SpringClassPathResourceOr standardClassLoaderCome to readresourcescontents of the file.

Method 1: UseClassPathResource

import ;
import ;
import ;
import ;
public class FileUtil {
    public static String readFileFromResources(String fileName) {
        StringBuilder content = new StringBuilder();
        try {
            // Get file resources            ClassPathResource resource = new ClassPathResource(fileName);
            try (BufferedReader reader = new BufferedReader(
                    new InputStreamReader((), StandardCharsets.UTF_8))) {
                String line;
                while ((line = ()) != null) {
                    (line).append("\n");
                }
            }
        } catch (Exception e) {
            ();
        }
        return ();
    }
}

Method 2: UseClassLoader

import ;
import ;
import ;
public class FileUtil {
    public static String readFileFromResources(String fileName) {
        StringBuilder content = new StringBuilder();
        try {
            // Get the class loader of the current thread            ClassLoader classLoader = ().getContextClassLoader();
            try (BufferedReader reader = new BufferedReader(
                    new InputStreamReader((fileName), StandardCharsets.UTF_8))) {
                String line;
                while ((line = ()) != null) {
                    (line).append("\n");
                }
            }
        } catch (Exception e) {
            ();
        }
        return ();
    }
}

Example usage

public class Main {
    public static void main(String[] args) {
        String content = ("mock_data.txt");
        (content);
    }
}

Things to note

File path

  • If the file is locatedresourcesIn the subdirectory ofdata/mock_data.txt,SofileNameShould be set to"data/mock_data.txt"

File existence

  • make suremock_data.txtHas been correctly placed insrc/main/resourcesor its subdirectory.
  • If the file does not exist or the path is wrong, it will be thrownFileNotFoundException

Coding issues

  • The character set of the file should be specified when reading (such asUTF-8) to avoid garbled code problems.

With these methods, you can easily read and reference Spring Boot projectsresourcesFile contents in the directory.

This is the end of this article about reading mock data (efficient debugging interface) in SpringBoot. For more related SpringBoot, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!