Author: Mintimate

Blog: www.mintimate.cn

Minxml’s Blog, just to share with you

Static files

Static resources, usually on the web:HTML files, JavaScript files, and images. especiallySet the static resource for the image, especially important:Such static resource access is not intercepted by Springboot (convenient for CDN acceleration) :

Although in real projects, images can be stored directly in buckets of object storage or reverse generation directly with Nginx, for small static resources, planning static resources directly with Springboot is also a good choice.

There are two main methods for setting static resources in Springboot, or static resource folders (both implemented by SpringMVC) :

  • inapplication.yml/application.propertiesIn the configuration.
  • Set up theThe Configuration Configuration class.

For more information, please refer to the official Spring documentation: www.baeldung.com/spring-mvc-…

The above two methods can achieve user access to the website, without the interception of the Controller layer, directly to the static file access:

Application setting method

The configuration,

Setting up the Application method is simple and involves two main configuration items:

  • spring.mvc.static-path-pattern: According to the description on the official website and the actual effect, it can be understood asStatic file URL matching headerThat is, the URL address of the static file. Springboot defaults to:/ * *.
  • spring.web.resources.static-locations: According to the description on the official website and the actual effect, it can be understood asActual static file address, that is, the actual static file that matches the static file URL. Springboot defaults to:classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

How does it work? Here I draw a simple diagram:Note:

  • Spring, web resources. The static – locations are subsequent configuration, the old Springboot configuration items for: spring – resources – static – locations; After 2.2.5, the configuration of the earlier version is invalid.
  • Spring, web resources. The static – locations with multiple configuration items, after Springboot compiler, will merge into one file. Multiple configuration files are used.Divide it up.
  • Spring, web resources. The static – the location allows only one configuration, cannot use.Divide it up,If you need more than one static resource file, you can use the config class method below.
  • Spring, web resources. The static – locations can be usedclasspath,fileMake a match. If file is used, the relative path is the project address (packaged as.jar, the relative path is the.jar run address).

Write the configuration

Now, with the official description, we know what configuration items mean. Now let’s configure it.

I used the APPLICATION configuration in YML format. If you use the APPLICATION. Properties in XML format, make sure to change it.

The end result is very simple. I want it to look like this:http://localhost:8088/SystemData/UserData/Avatar/Mintimate.jpegDirect access to the project files under:/SystemData/UserData/Avatar/Mintimate.jpeg To achieve this, we write a configuration file:

spring:
  mvc:
  	# URL response address (Springboot defaults to /**)
    static-path-pattern: /SystemData/**
  web:
    resources:
      # static file address, retain the official content, to append
      static-locations: classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources,file:SystemData
Copy the code

File :SystemData is a mapping local file.

This configuration is similar to Nginx’s re matching:

location ^~/SystemData{
	alias /www/myWeb/SystemData;
}
Copy the code

This way, we can run the project and access static resources directly:Of course, there are some downsides…

The advantages and disadvantages

This configuration, arguably the simplest and most crude, is slightly less flexible:

  • The URL response address can only be one item, i.espring.mvc.static-path-patternOnly one configuration item can be written.

This means that, as I set /SystemData/** to match the URL, I cannot set the second /resources/** configuration as the second static directory.

If you want to set multiple addresses as static resource directories, refer to the following section to set configuration class methods.

Set the configuration class method

The configuration,

Write a configuration class that implements a lot of folder methods for static resources. Such as:

  • Inheritance inWebMvcConfigurationSupportParent class, and implementaddResourceHandlersMethods.
  • referenceWebMvcConfigurerInterface, and implementaddInterceptorsmethods

Some articles might tell you to inherit the WebMvcConfigurerAdapter method, but the WebMvcConfigurerAdapter method has been deprecated since Spring5.0 and Springboot2.0.

Here, I in the habit, using WebMvcConfigurationSupport implement addResourceHandlers:

@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {}Copy the code

Here registry uses chained programming as follows:

  • addResourceHandler: Adds the URL response address directory.
  • addResourceLocations: Adds the actual resource directory.

Support keywords like classpath and file as set in application.yml. Next, let’s look at actually writing the configuration.

Write the configuration

Now let’s configure it. The end result is very simple, the effect I want (both groups at the same time) :

  • Browser input:http://localhost:8088/SystemData/UserData/Avatar/Mintimate.jpeg

Can direct access to the project file: under/SystemData/UserData/Avatar/Mintimate jpeg,

  • Browser input:http://localhost:8088/SystemDataTest/UserData/Avatar/Mintimate.jpeg

Can direct access to the project file: under the/Test/UserData/Avatar/Demo. The jpeg,

Add a configuration class and inheritWebMvcConfigurationSupportTo realizeaddResourceHandlersMethod, and type@ConfigurationAnnotation to make it a configuration class:Then, rewrite the content:Main is:

// Static resource mapping
registry.addResourceHandler("/SystemData/**")
        .addResourceLocations("file:"+IMG_PATH);
registry.addResourceHandler("/SystemDataTest/**")
        .addResourceLocations("file:"+IMG_PATH_TWO);
Copy the code

After that, the browser is ready to access:Nginx configuration class is the same as Nginx configuration class. This configuration is similar to Nginx’s re matching:

location ^~/SystemData{
	alias /www/myWeb/SystemData;
}
Copy the code

Of course, such advantages and disadvantages…

The advantages and disadvantages

Compared to the previous configuration, the advantages and disadvantages of this are obvious:

  • This configuration is more troublesome than the previous one.
  • This is more malleable than the previous article: more mappings can be added, default configurations are not overwritten, and so on.

conclusion

This is Springboot’s static resource directory addition method. Is it similar to Nginx?

Although Nginx and object storage are now convenient, using Springboot to partition static resources is not an option.