Spring MVC static resources in separate jar

2019-03-01 20:28发布

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,

1条回答
The star\"
2楼-- · 2019-03-01 21:01

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/");
}
查看更多
登录 后发表回答