What is the difference between Spring REST service, Jersey REST service and Spring+Jersey solutions?

Terry picture Terry · Nov 9, 2014 · Viewed 40k times · Source

I want to build a restful service/API. I used some framework like play to build it but I want to try other more efficient ways. I heard that Jersey is a common library for building the rest API, and Spring is also a good framework. But I also saw some solutions like Spring+Jersey. Thus, I am a little confused about those rest API solutions.

Can anyone tell me what is the difference among those? Jersey REST, Spring Rest and Spring+Jersey REST?

My goal is building a couple of rest API that take JSON as input/output. I have jar file as the backend process logic to process the input JSON/object and return JSON/object.

Answer

Ross Taylor-Turner picture Ross Taylor-Turner · Nov 10, 2014

Jersey is the JAX-RS API example implementation provided by Sun, while Spring REST is of course Spring's implementation of the same API/JSRs. The major difference is that Spring REST easily integrates into other Spring APIs (if you wish) such as Spring Data Rest.

There are a few noteworthy differences between them - you can "embed" Jersey Resources (known in Spring as Controllers) within each other, to enable a separate class that is responsible for the sub-path of a certain path, while this doesn't appear to be available in Spring right now (you have to define the full path). Also, in my opinion Jersey gives better "out of the box" error responses (such as why it can not map a JSON payload to a Java bean using Jackson) while Spring is a bit more configurable but plainer without some additional work.

In the end the difference in choosing between them usually comes down to - are you already or do you plan to integrate any other Spring libraries into your application? If so Spring REST is the way to go as you'll have a much easier time integrating it, otherwise it is really just personal preference which you'd prefer to use. Personally I like Jersey but the power of other related Spring projects (such as Spring HATEOAS which I highly recommend) makes Spring the better choice. I don't think there will be a real determining factor in your case.

As your "gold" target is a simple API with JSON input/output, I'd recommend you follow the Spring REST guide.