简体   繁体   中英

Webjars + Springboot. Custom resource path

I have a Spring Boot application which acts as a server for my frontend, built by webpack and included into my spring boot web archive.

I use webjars to access my frontend scripts and contents.

But there is one problem. To access webjars resources I need to use pathes like:

/webjars/jar-file-name/resource-name.ext

When in my react-js frontend code I use relateve pathes:

/resource-name.ext

I want to rebind paths of webjars to serve all resources /** from /webjars/jar-file-name

I have used this do to do it https://www.webjars.org/documentation#springmvc , but this seems to not work with Spring Boot

@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    super.addResourceHandlers(registry);
    registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/webjars/jar-file-name/");
  }
}

It should work with Spring MVC, but don't work in Spring Boot.

Could you please advice the right way to do it?

I think the following code would resolve your issue:

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  }
}

Here is my static content serving URL for bootstrap 3.1.0 from tutorial : http://localhost:8080/bootstrap/3.1.0/css/bootstrap.min.css

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM