Wednesday, August 7, 2013

having trouble with jersey 2.0 and servlet 3.0? you need jersey-core-servlet!

a subtle problem

I just came across this with some example code I'm working on, and the problem is easy to miss.

Let's say you want to use the JAXRS @ApplicationPath annotation for your Jersey application, and you don't want to use a web.xml file anymore, i.e. you want to programmatically define your servlet using Servlet 3.0 mechanisms. You have everything set up just like you've seen in all of the examples online, you run mvn jetty:run, and... Nothing.

two dependencies

There are two dependencies that serve similar purposes; adapting Jersey 2.0's Application class to a servlet instance. One of them includes compatibility for Servlet 2.x, and the other doesn't:

You'd think that this just means one has the ability to support Servlet 2.x and the other doesn't. In my experience, the case is that automatic Servlet 3.0 hooks won't actually work with jersey-container-servlet-core at all; it only works using jersey-container-servlet. Amusingly, the comment in the pom that is generated by the Jersey archetype (not the Grizzly one) is, at least in my mind, equally misleading:

In my mind, it makes more sense for this to say:


I set up a very basic example of this behavior that you can feel free to mess around with if you like. The following two classes make up what is just about the most basic example for a Jersey app (though I use ResourceConfig instead of Application just because I like its flexibility better):

To make this work, here's the pom we're going to use. In comments in the dependencies section, you can see which line you need to comment to experience the problem:

simple, but not so obvious, solution

Hopefully this helps you. This actually had me scratching my head (which means grinding my teeth as I gradually type harder) for the better part of a day before I realized I'd been bamboozled by similar dependencies.


  1. Thanks for this. I wasted a lot of time on this issue before finding your post.

    1. Glad I could help :)

      Often times my process is exactly that: if I've wasted a lot of time on a problem but found an answer, someone else is probably wasting time on it too and could use some help.

    2. IEEE Final Year Project centers make amazing deep learning final year projects ideas for final year students Final Year Projects for CSE to training and develop their deep learning experience and talents.

      IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation.

      corporate training in chennai corporate training in chennai

      corporate training companies in india corporate training companies in india

      corporate training companies in chennai corporate training companies in chennai

      I have read your blog its very attractive and impressive. I like it your blog. Digital Marketing Company in Chennai Project Centers in Chennai

  2. You're awesome. This drove me nuts until I came across your post. Thanks!!

    1. You're awesome for giving me some feedback; thanks! Same here; I was losing my mind on this for hours.

  3. Hi,
    Can you please share the actual project or point me to git, i just cant get it to work.

  4. It is difficult to trace the history of the teddy bear, because it appears to have been invented independently at around the same time in America and in Germany most famous teddy bears

  5. I think this is the best article today. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future reference.
    Java Courses in chennai

  6. Baseball jerseys are shirts preferably worn when playing baseball. They are either nabbed or V-necked and a few styles can be fastened from the front. derek jeter jerseys

  7. then proceed to register in older to attain the free bet.official site Most of these websites will have a 'free sport bet code,'

  8. To be positive that you have appropriately switched the picture, print a duplicate and put it in front of a mirror. This is the manner in which it will look on your customized t-shirt. custom shirts cheap

  9. problem still persist with weblogic 12.2.1. Not resolved.