babell00
babell00

Reputation: 81

Spring MVC static resources in separate jar

Recently I had a question from my co-workers if we can put our static resources(css, images, js) in separate jar file and access this static resources from spring mvc application. I was looking on google but i didn't find anything interesting. So my question is, is possible? and if so, can you please explain who to do it.

Thank you,

Upvotes: 2

Views: 1221

Answers (1)

Evgeni Dimitrov
Evgeni Dimitrov

Reputation: 22496

Spring Boot servers the static resources form classpath:/resources/, classpath:/static/ and classpath:/public/ (and some more). So if you create a jar file with one folder in it("public" for example) with all your static resources and put that jar in the classpath, all that content will be served.

If you don't use Boot, you can configure your application to serve the resources from that folder:

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

Upvotes: 1

Related Questions