This is the sixth day of my participation in the August Text Challenge.More challenges in August
The feed
Because the project needs to display other logs in the server disk, so we plan to use Spring Boot static resource mapping mode to display files on the page, convenient to download problems just can learn some wave of partial source code
Code configuration
` ` `package com.hgf.boot.config;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/** * Spring MVC configuration ** /
@Configuration
public class SpringMvcConfiguration implements WebMvcConfigurer {
/** * Static resource mapping */
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// Note that addResourceLocations must end with /
registry.addResourceHandler("/work_doc/**").addResourceLocations("file:/Users/hans/work_space/work/work/"); }}Copy the code
Source code analysis
In fact, the whole link is divided into two parts
- How do I fit request
/work_doc/**
Request to the folder/Users/hans/work_space/work/work/
Mapping preservation - If you visit
Mapping preservation
ResourceHandlerRegistration#addResourceLocations
public ResourceHandlerRegistration addResourceLocations(String... resourceLocations) {
this.locationValues.addAll(Arrays.asList(resourceLocations));
return this;
}
Copy the code
At this time on this. Where locationValues is called, ResourceHandlerRegistration# getRequestHandler
protected ResourceHttpRequestHandler getRequestHandler(a) {
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
if (this.resourceChainRegistration ! =null) {
handler.setResourceResolvers(this.resourceChainRegistration.getResourceResolvers());
handler.setResourceTransformers(this.resourceChainRegistration.getResourceTransformers());
}
handler.setLocationValues(this.locationValues);
if (this.cacheControl ! =null) {
handler.setCacheControl(this.cacheControl);
}
else if (this.cachePeriod ! =null) {
handler.setCacheSeconds(this.cachePeriod);
}
return handler;
}
Copy the code
Step by step, you can see the following call link
WebMvcConfigurationSupport#resourceHandlerMapping
ResourceHandlerRegistry#getHandlerMapping
ResourceHandlerRegistration#getRequestHandler
Copy the code
/ / register handlerMapping
@Bean
public HandlerMapping resourceHandlerMapping(a) {
Assert.state(this.applicationContext ! =null."No ApplicationContext set");
Assert.state(this.servletContext ! =null."No ServletContext set");
ResourceHandlerRegistry registry = new ResourceHandlerRegistry(this.applicationContext,
this.servletContext, mvcContentNegotiationManager(), mvcUrlPathHelper());
addResourceHandlers(registry);
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
if(handlerMapping ! =null) {
handlerMapping.setPathMatcher(mvcPathMatcher());
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
handlerMapping.setInterceptors(getInterceptors());
handlerMapping.setCorsConfigurations(getCorsConfigurations());
}
else {
handlerMapping = new EmptyHandlerMapping();
}
return handlerMapping;
}
Copy the code
If you visit
The Spring MVC flow network diagram is as follows
Combining the registration to the spring container BeanHandlerMapping, can probably guess the process org. Springframework. Web. Servlet. DispatcherServlet# doDispatch (omitted)
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {...try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
// Get the processor mapper
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null) {
noHandlerFound(processedRequest, response);
return;
}
// Get the processor adapter
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Call handler (handle request details source code)mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); . }// Render returnsprocessDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException); }... }Copy the code
The detailed call stack is shown
getResource:186, PathResourceResolver (org.springframework.web.servlet.resource)
getResource:157, PathResourceResolver (org.springframework.web.servlet.resource)
resolveResourceInternal:136, PathResourceResolver (org.springframework.web.servlet.resource)
resolveResource:48, AbstractResourceResolver (org.springframework.web.servlet.resource)
resolveResource:61, DefaultResourceResolverChain (org.springframework.web.servlet.resource)
getResource:529, ResourceHttpRequestHandler (org.springframework.web.servlet.resource)
handleRequest:439, ResourceHttpRequestHandler (org.springframework.web.servlet.resource)
handle:53, HttpRequestHandlerAdapter (org.springframework.web.servlet.mvc)
Copy the code
If there is a problem in the project, follow this process step by step to find the problem