errata
If I configure WebMvcConfigurer, static resources can still be accessed. !
preface
Although it is popular to separate the front-end and back-end deployment, sometimes front-end and back-end files need to be packaged together and distributed, which involves springBoot static resource access issues. Not only is it static resource packaging, such as using a local directory for file storage, but it can also be configured through the WebMvcConfigurer interface.
There is also a cross-domain problem when interacting with the front end. Cross-domain problems can also be solved through the WebMvcConfigurer interface.
Springboot default static file directory
Spring Boot provides static resource handling by default, so I recommend you use the default configuration of Spring Boot. The default static resource mappings are as follows:
- classpath:/META-INF/resources
- classpath:/resources
- classpath:/static
- classpath:/public
The static resources of these directories are directly accessible. These are static resource mapping paths. The priorities are meta-INF /resources > Resources > static > public
The SpringBoot project created using the Spring Initilize tool from Lecture 1 also creates the classpath:/static directory by default
Let’s put an image in this directory, head.jpg
Through the browser to http://localhost:8080/head.jpg
Custom configuration Static resource configuration
These are all provided by springBoot by default, so do we have the ability to customize a static resource map? Absolutely. Use the WebMvcConfigurer class to implement a custom static resource configuration.
We create a new WebConfig class that implements the WebMvcConfigurer interface as follows:
@Configuration
public class WebConfig implements WebMvcConfigurer {
}
Copy the code
We now what all don’t configuration, and then restart the application, visit http://localhost:8080/head.jpg. Instead, the program reported 404:
This is because we configured WebMvcConfigurer, which replaces the default configuration of SpringBoot. Here we want to customize a static resource configuration, for example, our /file/** requests are directed to my D drive root directory. We override the addResourceHandlers method of the WebMvcConfigurer interface. As follows:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/file/**")
.addResourceLocations("file:D:\");
}
Copy the code
Then put the head.jpg image on disk D:
Note that external resources are configured using the File declaration and internal JAR packages are configured using the CLASspath declaration.
The WebMvcConfigurer interface solves cross-domain problems
You can configure cross-domain problems by overriding the addCorsMappings method of WebMvcConfigurer. As follows:
Public void addCorsMappings(CorsRegistry registry) {registry. AddMapping (** * public void addCorsMappings(CorsRegistry registry) {"/ * *")
.allowedOrigins("*")
.allowCredentials(true)
.allowedMethods("GET"."POST"."DELETE"."PUT"."PATCH")
.maxAge(3600 * 24);
}
Copy the code
conclusion
The WebMvcConfigurer interface is not only capable of configuring static resource access and cross-domain problems, it can also configure many Web request-related things, such as interceptors, data converters, etc., which I won’t discuss here because of space. Things that can be configured can be viewed through the interface method as follows:
SpringBoot does a lot of things for us by default, which makes development a lot easier. However, sometimes we want to define our own handlers, Interceptor, ViewResolver, MessageConverter, Prior to Spring Boot 2.0, the WebMvcConfigurerAdapter method was overridden to add custom interceptors, message converters, and so on. After SpringBoot 2.0, this class is marked as @deprecated. For now, we have to implement the WebMvcConfigurer interface.