2

I'm working on a web service using spring-boot-starter-jersey and spring-boot-starter-tomcat (v. 1.5.2) and as such, I'd rather not add spring-boot-starter-web and further complicate my configuration. I want to stick the Swagger UI static files in a folder and serve them from there.

What I'm wondering is, can I serve static files using just spring-boot-starter-tomcat? I've found Spring documentation saying that I can server static content from a variety of sources on the classpath, but the examples I've found seem to require Spring MVC. I've tried disabling Jersey and putting static files in src/main/resources/static to test just Tomcat, but when I go to localhost/index.html, I get a 404 not found error.

As you might be able to tell from my path, I'm using Maven.

Since you can serve static files with just Tomcat, it seems like I should be able to serve static files with spring-boot-starter-tomcat. If this is the case, where do I put those files?

To put this another way, say I have started with the Spring-provided spring-boot-sample-jersey project. I have a requirement that the Jersey web service answer calls to the root address (/). How would I add some static content (HTML, CSS, JS) to be served from subdirectory called /swagger?

k-den
  • 853
  • 13
  • 28

1 Answers1

1

So the default servlet (which serves static content) is by default registered. But it will use only search specific paths as the document root. I had to go digging through source code to finally find it. If you look in the AbstractEmbeddedServletContainerFactory, you'll see

private static final String[] COMMON_DOC_ROOTS = {
        "src/main/webapp", "public", "static" };

If we don't explicitly set the document root, the above three are the paths that will be searched. In order, the first directory found that exists, will be used as the document root. I've verified that all of these work.

If you want to set a different directory, you can use a customizer bean

@Bean
public EmbeddedServletContainerCustomizer tomcatCustomizer() {
    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            container.setDocumentRoot(new File("src/main/resources/static"));
        }
    };
}

The one thing I haven't figured out is if we can serve files as classpath resources, instead of file system resources. If you look at the source code I linked to, it has some code that looks for the existence of a META-INF/resources. I thought that might work, but unfortunately it didn't for me. Maybe some guys of the Spring Boot team can enlighten us.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • This and the answer you gave at http://stackoverflow.com/questions/29658240/spring-boot-jersey-allow-jersey-to-serve-static-content has enabled me to add Swagger UI to my Spring Boot + Jersey webapp without adding Spring Boot Web. Thanks! Now if I can just find away to work around Swagger UI's non-support of Digest Authentication. – k-den Apr 22 '17 at 03:27