0

I am trying to add a resource handler into my Spring Boot App (1.1.8.RELEASE) which must be able to convert this example.

//From: {HOST_PREFIX_1}/u/{1}/{2}/myimage.jpg
//To:   {CDN_PREFIX_2}/u/{1}/{2}/images/myimage.jpg

I don't know whether spring is able of dealing with this kind of patterns or I have to create a custom resourceHandler.

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) 
{
   registry.addResourceHandler("/u/{var1}/{var2}/{var3:.*}")
            .addResourceLocations("/u/{var1/{var2}/images/{var3:.*}")
            .addResourceLocations(cdnPrefix);

    super.addResourceHandlers(registry);
}

Spring does not detect any kind of pattern error but the redirection is not working.

Any help?

Thanks!

3
  • I have finally made it work sending the request to a controller that makes the redirection logic but it would be great if anyone has a more elegant solution. Commented Nov 28, 2014 at 11:18
  • what do you mean with "host prefix" and "cdn prefix"; are those actual domains like example.org and mycdn.com? Commented Dec 4, 2014 at 9:11
  • Yes, that is what I meant Commented Dec 4, 2014 at 9:16

1 Answer 1

1

ResourceHandlers are designed to serve resources from the application, not send HTTP redirects.

If you'd like to send redirects based on patterns, then there are several solutions:

Use custom controller logic

This is what you ended up doing; useful if you need other features as well, such as authentication, etc.

Use a Filter

Such as URLRewriterFilter.

Better, write those links right within your template

Is there a particular reason for using HTTP redirects? This is a huge performance issue for your HTTP clients and somehow cancels the performance boost given by the CDN.

Depending on the templating engine you're using, you could do that at the templating level.

Sign up to request clarification or add additional context in comments.

1 Comment

Our application is a simple thumbnailer used by a bunch of old deprecated systems so the third approach does not fit into our requirements. Thanks for such as complete answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.