Jersey and Spring Boot Standalone JAR Files

By | December 6, 2016

Now ladies and gentlemen! It is time for a debugging break in the series of articles about the REST service!
Actually, this article is related to the REST service – it will describe a problem with the classpath-scanning of Jersey that manifest itself in standalone Spring Boot applications with a so-called fat JAR. I will also suggest a workaround that avoids scanning of the classpath.

The code snippets in this article were taken from the REST example program that can be found on GitHub.

Spring Boot Jersey

Jersey Configuration

This is the Jersey configuration that, in my standalone Spring Boot application, will cause the problem to manifest:

Build the Standalone Application

If I build the application using mvn clean package and then go into the target directory of the project, I see the following:

In my case the standalone Spring Boot application is in the largest JAR file, the rest-example-0.0.1-SNAPSHOT.jar.

Exceptions Galore

Trying to run the standalone Spring Boot application, using java -jar rest-example-0.0.1-SNAPSHOT.jar, the error below will occur. I have deleted all but the last parts of the stacktrace.

The reason for this error is a bug in Jersey. The JIRA issue for the bug can be found here. On the Spring side, there is two year old issue here.
There is a suggested workaround for Spring Boot which proposes unpacking JAR-files that contain classes that are to be scanned by Jersey. However, if you are in my situation where the package to be scanned is part of your own application and you, like me, don’t quite feel like extracting those classes to a separate JAR-file I will propose another solution.

New Jersey Configuration

To avoid having Jersey to perform a classpath scan, the resource classes, in my case, can be registered explicitly. The new JerseyConfig class in my REST example program project looks like this:

Rinse and Rebuild

Rebuild the application using mvn clean package and you should now be able to run the Spring Boot standalone JAR without the previous error.

Happy coding!

3 thoughts on “Jersey and Spring Boot Standalone JAR Files

  1. Dee

    Thats awesome — thanks a lot! This solution is really much more helpful then the (cumbersome) one with unpackaging JARS


Leave a Reply

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