Skip to content

Improve documentation on ProtocolResolvers and ResourceLoaders. [SPR-14671] #19235

Closed
@spring-projects-issues

Description

@spring-projects-issues

Petar Tahchiev opened SPR-14671 and commented

This is a follow-up on:

#17455

There @Brian Clozel mentioned that I can

"create your own ResourceResolver implementation and configure it in the chain."

I have done so:

public class MediaResourceResolver implements ResourceResolver {

    @Override
    public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
        LOG.info("TADAA");
        return null;
    }

    @Override
    public String resolveUrlPath(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain) {
        LOG.info("TADAA");
        return null;
    }
}

but I can't find anywhere on the internet how to register my custom ResourceResolver "in the chain".

Brian also said:
"You can even use a DefaultResourceLoader with custom ProtocolResolvers if this might help you."

and this was my second try. I created:

public class MediaProtocolResolver implements ProtocolResolver {
    @Override
    public Resource resolve(String location, ResourceLoader resourceLoader) {

        if (location.startsWith(MEDIA_URL_PREFIX)) {
            LOG.info("TADAA");
        }
        return null;
    }

}

and registered it like this in my spring-boot application:

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!registry.hasMappingForPattern("/media/**")) {
            applicationContext.addProtocolResolver(new MediaProtocolResolver());
            registry.addResourceHandler("/media/**").addResourceLocations("media:/media/").setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
        }
        super.addResourceHandlers(registry);
    }

but nothing was printed on command line. Can you please improve the documentation on ProtocolResolvers and ResourceLoaders.

Thank you.


Issue Links:

Metadata

Metadata

Assignees

Labels

status: declinedA suggestion or change that we don't feel we should currently applytype: taskA general task

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions