0

I tried many solution and stackoverflow is last place that I can ask about my problem. I've created application with Spring Boot on backend that also serves my frontend. My HomeController looks as follows

@Controller
public class HomeController {

    @RequestMapping(value = "/*",  method={RequestMethod.GET, RequestMethod.HEAD})
    public String fallback() {
         return "index";
    }
 }

And here are some antMatchers:

             http.csrf().disable()
            .authorizeRequests()
            .antMatchers("/resources/**", "/built/**").permitAll()
            .antMatchers("/css/**").permitAll()
            .antMatchers(HttpMethod.POST, "/login").permitAll()
            .antMatchers("/registration","/about","/garages/**/*").permitAll()
            .antMatchers("/", "/home").permitAll()

Everything works perfect when I'm trying to reach

/registration, /about or just /

but when I'm trying to get

/garages/5

I see empty page and in Network Tab (in mozilla) it show that bundle is trying be taken from

http://localhost:8080/garages/built/bundle.js And status 404

Which for /about page it looks as follows:

http://localhost:8080/built/bundle.js (this is correct one).

Is anybody here able to point any issue that I'm constantly somehow ommitting which causes my problem with proper redirection?

1 Answers1

0

EDIT If you have your static assets on the resources/static folder, this works:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**/*")
            .addResourceLocations("classpath:/static/")
            .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("/static/index.html");
                }
            });
    }
}

Solution credits and further explanation:

https://stackoverflow.com/a/46854105

SrThompson
  • 5,568
  • 2
  • 17
  • 25
  • @MichalCholewiński the edit is what I'm planning to use on my own project. Please let me know if it worked for you (I did a quick test and it seems to respond well to sub-paths on the frontend) – SrThompson Jan 24 '18 at 15:36
  • It also didn't help but I solved my problem by adding @RequestMapping(value = {"/*","/garages/*"}, method={RequestMethod.GET, RequestMethod.HEAD}). When I had '"/**" as you said on beginning the wrong handler had been taken when website made call for bundle.js – Michal Cholewiński Jan 25 '18 at 07:42
  • Yeah, I saw that solution somewhere... the alternative is to just put the matchers for every client route/subroute on @RequestMapping. All these alternatives seem a bit hacky to me, but as long as they work I guess it's not a big deal – SrThompson Jan 25 '18 at 12:33