Get the App
SLTechnology News&Howtos  ›  Development  › 

How to configure static paths for spring boot

Shulou Source: shulou.com Published: 2022-06-01 17:36:25 09月18日 Update

This article mainly introduces the spring boot how to configure the static path of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this spring boot how to configure the static path, let's take a look.

A preface

Spring boot for static files can be achieved by configuring the path.

@ Slf4j@Configurationpublic class WebMvcConfig extends WebMvcConfigurationSupport {@ Resource private PermissionInterceptor permissionInterceptor; @ Resource private CheckOpenInterceptor checkOpenInterceptor; @ Value ("${dataDir}") private String rootPath; @ Override protected void addResourceHandlers (ResourceHandlerRegistry registry) {log.info ("> addResourceHandlers > >" + "file:" + rootPath); registry.addResourceHandler ("/ file/**"). AddResourceLocations ("file:" + rootPath); super.addResourceHandlers (registry) } @ Override public void addInterceptors (InterceptorRegistry registry) {registry.addInterceptor (permissionInterceptor) .addPathPatterns ("/ file/**"); super.addInterceptors (registry);}}

RootPath my local debugging environment is: d:/file/

The test environment is / data/file

/ * * ": indicates the access path, which is specified according to the actual situation (here all paths under / file/)

"file:/d/file/": indicates the real storage location of static resources on the hard disk, specified according to the actual situation

Local development is very smooth, soon written, when deployed to the test machine, the uploaded file, can not be previewed, has been prompted 404.

Second, the long road of debugging

The initial suspicions are two questions.

1. The permission problem checks that the file is also written and read normally, and the permission problem is excluded.

2 the problem of file coding

I uploaded a Chinese directory, maybe it could not be mapped in Chinese. Later, I uploaded an all-English file, but I couldn't preview it. It was really hematemesis.

It won't solve my problem.

I really couldn't think of any problem. I considered whether it was possible to use debug remotely.

I configured ide debug, the remote java startup command

Java-agentlib:jdwp=transport=dt_socket,address=5555,server=y,suspend=y-jar myproject.jar

Ide configuration

Where host is my local ip address

Start the remote service, hit the breakpoint, and then debug

After several hours of spring debug source code, I finally found that my path was missing a / sign. Spring boot directly ignored the treatment of not / ending, and found the superior directory, which caused the reason of 404 all the time.

This is the end of the article on "how to configure static paths in spring boot". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to configure static paths in spring boot". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Path configuration static problem file knowledge content reality situation permission environment directory article test long superior value location preface reason Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Technology Huawei Xiaomi MySQL