Package org.springframework.web.servlet.config.annotation

# Class ResourceHandlerRegistry

java.lang.Object
org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry

public class ResourceHandlerRegistry extends Object
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 (for example, `"/resources/**"`).

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

Since:
  3.1
Author:
  Rossen Stoyanchev
See Also:
  - `DefaultServletHandlerConfigurer`

## Constructor Summary

Constructors
Constructor
Description
`ResourceHandlerRegistry(org.springframework.context.ApplicationContext applicationContext, ServletContext servletContext)`
Create a new resource handler registry for the given application context.
`ResourceHandlerRegistry(org.springframework.context.ApplicationContext applicationContext, ServletContext servletContext, org.springframework.web.accept.ContentNegotiationManager contentNegotiationManager)`
Create a new resource handler registry for the given application context.
`ResourceHandlerRegistry(org.springframework.context.ApplicationContext applicationContext, ServletContext servletContext, org.springframework.web.accept.ContentNegotiationManager contentNegotiationManager, org.springframework.web.util.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 Type
Method
Description
`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.
`boolean`
`hasMappingForPattern(String pathPattern)`
Whether a resource handler has already been registered for the given path pattern.
`ResourceHandlerRegistry`
`setOrder(int order)`
Specify the order to use for resource handling relative to other `HandlerMappings` configured in the Spring MVC application context.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### ResourceHandlerRegistry

public ResourceHandlerRegistry(org.springframework.context.ApplicationContext applicationContext, ServletContext servletContext)
Create a new resource handler registry for the given application context.

Parameters:
  `applicationContext` - the Spring application context
  `servletContext` - the corresponding Servlet context

### ResourceHandlerRegistry

public ResourceHandlerRegistry(org.springframework.context.ApplicationContext applicationContext, ServletContext servletContext, @Nullable org.springframework.web.accept.ContentNegotiationManager contentNegotiationManager)
Create a new resource handler registry for the given application context.

Parameters:
  `applicationContext` - the Spring application context
  `servletContext` - the corresponding Servlet context
  `contentNegotiationManager` - the content negotiation manager to use
Since:
  4.3

### ResourceHandlerRegistry

public ResourceHandlerRegistry(org.springframework.context.ApplicationContext applicationContext, ServletContext servletContext, @Nullable org.springframework.web.accept.ContentNegotiationManager contentNegotiationManager, @Nullable org.springframework.web.util.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 Details

### addResourceHandler

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.

### hasMappingForPattern

public boolean hasMappingForPattern(String pathPattern)
Whether a resource handler has already been registered for the given path pattern.

### setOrder

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`.

### getHandlerMapping

@Nullable protected AbstractHandlerMapping getHandlerMapping()
Return a handler mapping with the mapped resource handlers; or `null` in case of no registrations.