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

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

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

This is a part of Jersey (JAX-RS) Restful Web Services Development Guides series. Please read Jersey + Spring Boot getting started guide.

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 configurations 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 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 which Jersey can’t process (404).

Let say we have a file /src/resources/static/index.html, it accessible at http://<host>:<port>/index.html now.

References

  1. Official Jersey Documentation
  2. Spring Boot reference
Download Icon SmallDownload the Full ProjectGithub IconFollow Project On Github

Leave a Reply

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