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 use SpringBoot to access static resources

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

In this issue, the editor will bring you about how to use SpringBoot to access static resources. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Preface

According to years of development experience,    found that static resource services are essential for back-end projects that interact with the front end (for example, images and files). Large projects may have independent oss object storage file servers, while small projects are basically stored on local disks to access static resources (pictures, files) through back-end services. This chapter explains how to access static resources through backend services.

Use the default configuration of Spring Boot to access pictures

1. The default configuration of / * * is mapped to the folders / static, / public, / resources, / META-INF/resources,application.properties files in the src/main/resources/ directory in the project. Default configuration:

# static resource access path

Spring.mvc.static-path-pattern=/**

# static resource mapping path

Spring.resources.static-locations=classpath:/

two。 Directory priority: / META-INF/resources > / resources > / static > / public (I personally tested that each directory has a different picture but the file name is the same, and the access test draws a conclusion)

Launch and access: http://localhost:8080/1.jpg

Second, realize the interface WebMvcConfigurer to rewrite addResourceHandlers1. Add configuration class MyWebMvcConfigurer

Don't forget to add @ Configuration note!

Package com.example.demo.config

Import org.springframework.context.annotation.Configuration

Import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry

Import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@ Configuration

Public class MyWebMvcConfigurer implements WebMvcConfigurer {

@ Override

Public void addResourceHandlers (ResourceHandlerRegistry registry) {

Registry.addResourceHandler ("/ webmvc/**") .addResourceLocations ("classpath:/webmvc/")

}

}

Add a new webmvc directory to the resource directory and put the 1.jpg test images

two。 Start the project and access the resource mapping path: http://localhost:8080/webmvc/1.jpg describes two ways to achieve static resource access, one is the configuration method of SpringBoot itself (no code), and the other is to achieve static resource access by writing configuration classes to achieve Spring WebMvcConfigurer interface class path resource mapping. If there are no special needs, it is recommended to use SpringBoot configuration method. The above is the editor for you to share how to use SpringBoot to access static resources, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, 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

Servers

Wechat

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

12
Report