Top Description Fields Constructors Methods
org.springframework.web.servlet.config.annotation

public Class ResourceHandlerRegistry

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Arrays, .LinkedHashMap, .List, .Map, jakarta.servlet.ServletContext, org.springframework.beans.factory.BeanInitializationException, org.springframework.context.ApplicationContext, org.springframework.core.Ordered, org.springframework.lang.Nullable, org.springframework.util.AntPathMatcher, .Assert, org.springframework.web.HttpRequestHandler, org.springframework.web.accept.ContentNegotiationManager, org.springframework.web.servlet.HandlerMapping, org.springframework.web.servlet.handler.AbstractHandlerMapping, .SimpleUrlHandlerMapping, org.springframework.web.servlet.resource.ResourceHttpRequestHandler, org.springframework.web.util.UrlPathHelper, org.springframework.web.util.pattern.PathPattern

Stores registrations of resource handlers for serving static resources such as images, css files and others through Spring MVC including setting cache headers optimized for efficient loading in a web browser. Resources can be served out of locations under web application root, from the classpath, and others.

To create a resource handler, use addResourceHandler(String...) providing the URL path patterns for which the handler should be invoked to serve static resources (e.g. "/resources/**").

Then use additional methods on the returned ResourceHandlerRegistration to add one or more locations from which to serve static content from (e.g. {"/", "classpath:/META-INF/public-web-resources/"}) or to specify a cache period for served resources.

Author
Rossen Stoyanchev
Since
3.1
See Also
DefaultServletHandlerConfigurer

Field Summary

Modifier and TypeField and Description
private final ApplicationContext
private final ContentNegotiationManager
private int
private final UrlPathHelper
private final List<ResourceHandlerRegistration>
private final ServletContext

Constructor Summary

AccessConstructor and Description
public
ResourceHandlerRegistry(ApplicationContext
the Spring application context
applicationContext
,
ServletContext
the corresponding Servlet context
servletContext
)

Create a new resource handler registry for the given application context.

public
ResourceHandlerRegistry(ApplicationContext
the Spring application context
applicationContext
,
ServletContext
the corresponding Servlet context
servletContext
,
ContentNegotiationManager
the content negotiation manager to use
contentNegotiationManager
)

Create a new resource handler registry for the given application context.

public
ResourceHandlerRegistry(ApplicationContext applicationContext, ServletContext servletContext, ContentNegotiationManager contentNegotiationManager, UrlPathHelper pathHelper)

A variant of ResourceHandlerRegistry(ApplicationContext, ServletContext, ContentNegotiationManager) that also accepts the UrlPathHelper used for mapping requests to static resources.

Method Summary

Modifier and TypeMethod and Description
public ResourceHandlerRegistration
addResourceHandler(String... pathPatterns)

Add a resource handler to serve static resources.

protected AbstractHandlerMapping
getHandlerMapping()

Return a handler mapping with the mapped resource handlers; or null in case of no registrations.

private ResourceHttpRequestHandler
public boolean
hasMappingForPattern(String pathPattern)

Whether a resource handler has already been registered for the given path pattern.

public ResourceHandlerRegistry
setOrder(int order)

Specify the order to use for resource handling relative to other HandlerMappings configured in the Spring MVC application context.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

applicationContextback to summary
private final ApplicationContext applicationContext
contentNegotiationManagerback to summary
private final ContentNegotiationManager contentNegotiationManager
Annotations
@Nullable
orderback to summary
private int order
pathHelperback to summary
private final UrlPathHelper pathHelper
Annotations
@Nullable
registrationsback to summary
private final List<ResourceHandlerRegistration> registrations
servletContextback to summary
private final ServletContext servletContext

Constructor Detail

ResourceHandlerRegistryback to summary
public ResourceHandlerRegistry(ApplicationContext applicationContext, ServletContext servletContext)

Create a new resource handler registry for the given application context.

Parameters
applicationContext:ApplicationContext

the Spring application context

servletContext:ServletContext

the corresponding Servlet context

ResourceHandlerRegistryback to summary
public ResourceHandlerRegistry(ApplicationContext applicationContext, ServletContext servletContext, ContentNegotiationManager contentNegotiationManager)

Create a new resource handler registry for the given application context.

Parameters
applicationContext:ApplicationContext

the Spring application context

servletContext:ServletContext

the corresponding Servlet context

contentNegotiationManager:ContentNegotiationManager

the content negotiation manager to use

Since
4.3
ResourceHandlerRegistryback to summary
public ResourceHandlerRegistry(ApplicationContext applicationContext, ServletContext servletContext, ContentNegotiationManager contentNegotiationManager, UrlPathHelper pathHelper)

A variant of ResourceHandlerRegistry(ApplicationContext, ServletContext, ContentNegotiationManager) that also accepts the UrlPathHelper used for mapping requests to static resources.

Since
4.3.13

Method Detail

addResourceHandlerback to summary
public ResourceHandlerRegistration addResourceHandler(String... pathPatterns)

Add a resource handler to serve static resources. The handler is invoked for requests that match one of the specified URL path patterns.

Patterns such as "/static/**" or "/css/{filename:\\w+\\.css}" are supported.

For pattern syntax see PathPattern when parsed patterns are enabled or AntPathMatcher otherwise. The syntax is largely the same with PathPattern more tailored for web usage and more efficient.

getHandlerMappingback to summary
protected AbstractHandlerMapping getHandlerMapping()

Return a handler mapping with the mapped resource handlers; or null in case of no registrations.

Annotations
@Nullable
getRequestHandlerback to summary
private ResourceHttpRequestHandler getRequestHandler(ResourceHandlerRegistration registration)
Annotations
@SuppressWarnings:deprecation
hasMappingForPatternback to summary
public boolean hasMappingForPattern(String pathPattern)

Whether a resource handler has already been registered for the given path pattern.

setOrderback to summary
public ResourceHandlerRegistry setOrder(int order)

Specify the order to use for resource handling relative to other HandlerMappings configured in the Spring MVC application context.

The default value used is Integer.MAX_VALUE-1.