Get the App
SLTechnology News&Howtos  ›  Development  › 

How springboot accesses external resources through URL

Shulou Source: shulou.com Published: 2022-06-02 08:43:30 10月03日 Update

This article mainly explains "how springboot accesses external resources through URL". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how springboot accesses external resources through URL".

Springboot accesses external resources through URL

When you encounter this problem, there are only two ways to browse Baidu.

First kind

This method is out of date in springboot 2.1.8

The second kind

This method enables you to access the files in the specified directory through url, but using spring.resources.static-locations this configuration will overwrite the default static resource folder of SpringBoot, all the static resources of the project cannot be accessed, and the default value of spring.mvc.static-path-pattern is / * * without configuration.

The solution is to add a custom folder path to access in spring.resources.static-locations, which must start with file:, and add the default four static resource paths to access the project and custom static resources.

Custom static resource path is D:file/

Spring reported the error there because there is already a spring layer on it, which is added here to facilitate the demonstration.

Customize the files under the static resource folder

Download this picture of .jpg via url

At the same time, the project can also access

Springboot accesses the local file @ Configuration public class MyWebMvcConfigurer extends WebMvcConfigurationSupport {@ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {/ / access the relative path registry.addResourceHandler ("/ assets/**") .addResourceLocations ("classpath:/assets/") through URL / / access absolute path access path and storage path can be customized. It is recommended to put the storage path in the configuration file registry.addResourceHandler ("/ img/**"). AddResourceLocations ("file:D:/img/"); super.addResourceHandlers (registry);}} localhost:8080/assets/ file name. Suffix Thank you for your reading, the above is the content of "how springboot accesses external resources through URL". After the study of this article, I believe you have a deeper understanding of how springboot accesses external resources through URL, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Resources files paths methods static that is folders methods projects learning configuration content problems outdated nothing more all at the same time suffixes pictures suggestions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi Shulou Information vpn Apple