59

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 a 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 APIs that take JSON as input/output. I have jar file as the backend process logic to process the input a JSON/object and return a JSON/object.

Henke
  • 4,445
  • 3
  • 31
  • 44
Terry
  • 855
  • 4
  • 12
  • 16

3 Answers3

52

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.

Ross Taylor-Turner
  • 3,687
  • 2
  • 24
  • 33
  • 1
    Also Spring+Jersey REST usually refers to using Jersey for the RESTful endpoints with Spring for the rest of the application - when you wish to use Jersey annotations/implementation but other Spring frameworks such as dependency injection. – Ross Taylor-Turner Nov 10 '14 at 17:04
  • I think this sentence may be wrong: "Spring REST is of course Spring's implementation of the same API/JSRs". According to this other question's answer, Spring REST "is not a JAX-RS implementation and can be seen as a Spring alternative to the JAX-RS standard." This means there are significant and incompatible differences in the annotations each use. https://stackoverflow.com/a/42955575/7027725 – PeteH32 Dec 09 '21 at 20:10
10

One major difference is in the area of unit testing support.

Jersey Test Framework does not lend itself for mocking server side code - For example, if your REST Resource depended on a Service, you would like to mock the service when testing resource methods. However, Jersey Tests run a separate container and unit tests sort of make calls to the running instance of your REST resource - at this point of time, I have not found any documentation or way for mocking server side code.

On the contrary, Spring MVC tests do not require any containers - and are more well integrated with its controllers. Dependency Injection can be used to inject mock services / DAOs to have better unit tests.

I also find that documentation on Spring projects are more mature when compared to Jersey.

Wand Maker
  • 18,476
  • 8
  • 53
  • 87
  • Have a look at my answer here -> https://stackoverflow.com/questions/27430052/jersey-how-to-mock-service/32009541#32009541 to see how to mock server side code. – Lars Juel Jensen Feb 06 '20 at 11:55
0

One subtle difference is in the instantiation of the resource (Jersey) or controller (Spring) objects.

Jersey new's a resource object for each request. Whereas, by default Spring treats controllers as beans with default scope of singleton. This can be overridden with a @Scope annotation (although if you do that it will get flagged by Sonar).

This default behavior of Spring has bitten our application several times. With the controller class being a singleton, all class members are effectively static. So values set handling one request will still be there for the next.

This is something to be aware of if your using Spring. My suggestion is to @Scope the controller class as prototype, even though that will earn you a warning if you do Sonar scans.

  • 1
    No. This is bad practice. Servlets or Controllers are supposed to be Singleton. They should be just pass through to business/DAL layers. They are supposed to just decode the data coming from UI and pass it on. Keep them stateless. – Vineet Jun 23 '22 at 20:04