Rolf Rolf - 1 year ago 268
Java Question

Spring-Boot Jersey: allow Jersey to serve static content

The application uses JDK 8, Spring Boot & Spring Boot Jersey starter and is packaged as a WAR (although it is locally run via Spring Boot Maven plugin).

What I would like to do is to get the documentation I generate on the fly (at build time) as a welcome page.

I tried several approaches:

  1. letting Jersey serving the static contents by configuring in
    the proper init parameter as described here

  2. introduce a
    in order to list the generated HTML document as a welcome-file.

None of that worked out.

I would like to avoid having to enable Spring MVC or creating a Jersey resource just for serving a static file.

Any idea?

Here is the Jersey configuration class (I unsuccessfully tried to add a

public class ResourceConfiguration extends ResourceConfig {

public ResourceConfiguration() {
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);

And here is Spring Boot application class (I tried adding an
but it didn't work, I'm not exactly sure what the property key should be here):

public class Application extends SpringBootServletInitializer {

protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);

public static void main(String[] args) {, args);

Answer Source

Let me just first state, that the reason the static content won't be served is because of the default servlet mapping of the Jersey servlet, which is /*, and hogs up all the requests. So the default servlet that serves the static content can't be reached. Beside the below solution, the other solution is to simply change the servlet mapping. You can do that be either annotating your ResourceConfig subclass with @ApplicationPath("/another-mapping") or set the property spring.jersey.applicationPath.

In regards to your first approach, take a look at the Jersey ServletProperties. The property you are trying to configure is FILTER_STATIC_CONTENT_REGEX. It states:

The property is only applicable when Jersey servlet container is configured to run as a Filter, otherwise this property will be ignored

Spring Boot by default configures the Jersey servlet container as a Servlet (as mentioned here):

By default Jersey will be set up as a Servlet in a @Bean of type ServletRegistrationBean named jerseyServletRegistration. You can disable or override that bean by creating one of your own with the same name. You can also use a Filter instead of a Servlet by setting spring.jersey.type=filter (in which case the @Bean to replace or override is jerseyFilterRegistration).

So just set the property spring.jersey.type=filter in your, and it should work. I've tested this.

And FYI, whether configured as Servlet Filter or a Servlet, as far as Jersey is concerned, the functionality is the same.

As an aside, rather then using the FILTER_STATIC_CONTENT_REGEX, where you need to set up some complex regex to handle all static files, you can use the FILTER_FORWARD_ON_404. This is actually what I used to test. I just set it up in my ResourceConfig

public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        property(ServletProperties.FILTER_FORWARD_ON_404, true);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download