Spring Boot and Jersey (JAX-RS) static files support

Learn how to provide support of static files (HTML, JS, CSS, Images) in Spring Boot & Jersey project without any error.

In Spring Boot support for Jersey is provided by org.springframework.boot:spring-boot-starter-jersey Gradle dependency. When the static file URL pattern is matched with URLs that Jersey handles, additional configurations are required for Spring Boot to serve static resources.

Note: We are using Gradle dependency management system, configuration for Maven will be similar.

Add Spring Boot web starter dependency

File : build.gradle (Snippet)

dependencies {
     compile 'org.springframework.boot:spring-boot-starter-web',
     		 'org.springframework.boot:spring-boot-starter-jersey', 
    		 'org.springframework.boot:spring-boot-devtools'
    		 
    testCompile 'org.springframework.boot:spring-boot-starter-test'
}

Add org.springframework.boot:spring-boot-starter-web to enable Spring Web application support. We need @SpringBootApplication on Spring Boot main application class or @EnableAutoConfiguration. With these annotation Spring Boot will auto-configure serving of static content from /src/resources/static/ or /src/resources/public folders.

Unfortunately our Spring Boot server doesn’t serve static files as expected. It happens because of Jersey servlet, which is handling all http requests (/*) and doesn’t have clue about static files. If Jersey is only handling some http requests, for e.g. /api/* then this problem will not arise as long as static resources are not kept on /api/* path.

Jersey configuration

We have to make two Jersey configuration changes.

Use Filter to process request

Jersey can handle requests either using Servlet or using Filter. By default, Servlet is used. Make following changes so Filter is used.

File: application.properties

spring.jersey.type=filter

User Filter forward 404

If Jersey doesn’t have clue (HTTP status code 404) about the request, it should forward it for further processing.

File: JerseyConfig.java (snippet)

@Component
@ApplicationPath("/")
public class JerseyConfig extends ResourceConfig {

	/*
	 * In constructor we can define Jersey Resources & Other Components
	 */
	public JerseyConfig() {
		
		/*
		 * Jersey will automatically register class with @provider, @Component  by scanning
		 * these packages + nested packages
		 */
		packages("in.geekmj.resource", "in.geekmj.config");
		
		/* CustomTypeParamterConsumeResource auto scanned and register */
		//register(CustomTypeParamterConsumeResource.class);
		register(MatrixUriResource.class);
		register(RequestCookiesResource.class);
		register(RequestHeaderResource.class);
		register(RequestParameterResource.class);
		register(EmployeeResource.class);
		register(HumansResource.class);
		register(HelloWorldResource.class);
		property(ServletProperties.FILTER_FORWARD_ON_404, true);
	}
}

property(ServletProperties.FILTER_FORWARD_ON_404, true); will forward (to other Servlet or filters) all requests that Jersey can’t process (404).

Let’s say we have a file /src/resources/static/index.html, it is accessible at https://host:port/index.html now.

References

  1. Official Jersey Documentation
  2. Spring Boot reference
  3. Download the Full Project
  4. Follow Project On Github
JOIN OUR NEWSLETTER
And get notified everytime we publish a new blog post.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top