Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure static paths for spring boot

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report