0

I have a spring-boot application in which I redirect all resources to index.html, because my front-end is a SPA.

How ever I want to keep the call to localhost:8080/swagger-ui.html the same and overwrite all the others.

It seems that addResourceHandler doesn't work with regular expressions. Any ideas on how to solve this problem ?

import java.io.IOException;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**/*") //^\/[^(api|swagger)].*
                .addResourceLocations("classpath:/public/")
                .resourceChain(true)
                .addResolver(new PathResourceResolver() {
                    @Override
                    protected Resource getResource(String resourcePath, Resource location) throws IOException {
                      Resource requestedResource = location.createRelative(resourcePath);
                      return requestedResource.exists() && requestedResource.isReadable() ? requestedResource : new ClassPathResource("/public/index.html");
                    }
                })
                ;

    }

}

PS: I'm using spring fox with in memory swagger.

randomname
  • 159
  • 6

1 Answers1

0

You can instead use Spring Controller with @RequestMapping annotation which takes regex path.

//redirects all paths except one containing api or swagger references.
@RequestMapping(value = ""/**/{path:^(?!swagger|api).*}"")
public void redirect(HttpServletResponse response) throws IOException { response.sendRedirect("/"); }

Reference

Amith Kumar
  • 4,400
  • 1
  • 21
  • 28
  • Don't think the request mapping works with regex :/ – randomname Mar 15 '19 at 22:09
  • yes it does, you can find way many references on SO here. [Example 1](https://stackoverflow.com/questions/18422368/regex-in-spring-controller). – Amith Kumar Mar 15 '19 at 22:11
  • `@RequestMapping(value = "^\\/[^(api|swagger)].*")` doesn't work even though the regex is correct. – randomname Mar 15 '19 at 22:25
  • Your regex is incorrect, I have updated the answer with suggested regex & reference from spring doc for more details & learning. – Amith Kumar Mar 15 '19 at 23:51
  • Did my provided solution resolve you query ? Were you able to achieve the desired result ? Please see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). As the asker, you have a special privilege: you may accept the answer that you believe is the best solution to your problem. – Amith Kumar Mar 20 '19 at 00:32