Spring 4 vs Jersey for REST web services

87,562

Solution 1

I'd say both Jersey and Spring MVC are great - each project has its own style and strengths. Anyway, Stack Overflow is not the right place for asking subjective comparisons (your question would be closed quite quickly). If you're already using Spring for everything else and are not required to use JAX-RS, then Spring MVC makes total sense.

Regarding features like (un)marshalling, JAX-RS is just a spec after all - other libraries can offer similar features without implementing the same API.

  1. Instead of MessageBodyReaders/Writers, Spring MVC is using HttpMessageConverters to handle (un)marshalling REST resources. Spring MVC handles content negotiation and chooses the best available converter for the job (you can annotate methods to hint at what media type they produce/consume).

  2. No, it's not necessary to use JAX-RS to (un)marshall resources. In fact, JAX-RS implementations and Spring MVC use third party serialization libraries to do the job; so it's not tied to a particular standard.

  3. In its 4.0.6 version, Spring supports many HttpMessageConverters, with Jackson for JSON, JAXB for XML and many others. Spring 4.1.0 added more HttpMessageConverters:

To answer your last point, @XmlRootElement is a JAXB annotation and is not part of JAX-RS. Spring supports JAXB.

For a more complete example with REST in Spring, check out this getting started guide (you'll get a complete example running in 10-15 minutes).

Again the last part of your question is quite subjective - there are many popular solutions for building REST services in the JVM, not just Jersey and Spring (Dropwizard, Play! Framework, etc).

Solution 2

AFAIK Spring REST support is based on Spring MVC and its not JAX-RS implementation while Jersey has implemented JAX-RS specification. Those having Spring (Core, AOP or MVC) in their project chooses Spring ReST support over JAX-RS implementor.

I recommend Jersey as its mature, implements JAX-RS and is easy to use.

Share:
87,562
RSCode
Author by

RSCode

Updated on April 20, 2020

Comments

  • RSCode
    RSCode about 4 years

    We are planning to make a new application with spring 4.0.6 version. We use controller that can return "XML" or "JSON". In the previous project we have successfully implemented Jersey with Spring for REST support using JAX-RS API, but after reading a few articles and suggestions from seniors they said spring is providing pretty good REST support.

    Some of the points that really confused me if I use Spring REST support without using JAX-RS and Jersey are:

    1. How marshaling and unmarshaling done in Spring MVC ?

    2. Is it necessary for marshaling or unmarshaling need to use jax-rs.

    3. If marshaling and unmarshaling are automatically handled by spring then how does it know about xmlRootElements.

    I am still confused if Spring proving very good support of REST then why do people still go with Jersey for REST? Really looking to know more in details.

    If I said anything wrong please ignore it. Explanation with example is really helpful.

    Thanks in advance!!

  • RSCode
    RSCode over 9 years
    Thanks for reply, how marshaling and unmarshaling done in spring without using jax-rs.
  • Evan Hu
    Evan Hu over 9 years
    yeah, I agree with you. Spring Web MVC Rest is not that easy to use. There are many configurations you need to take care.
  • herau
    herau almost 9 years
    Jersey is also a good choice if you use (or you want to use) Spring boot. cf documentation
  • swade
    swade about 8 years
    Where would you suggest someone ask a question like this?
  • Tejzeratul
    Tejzeratul almost 8 years
    You can use Google GSON library for marshaling and unmarshaling. It supports Java generics and custom objects.
  • Afshin Moazami
    Afshin Moazami over 7 years
    "your question would be closed quite quickly"! Not that quick :)
  • Kalpesh Soni
    Kalpesh Soni about 7 years
    I have love stackoverflow and i love quora, i have never liked this closing subjective question policy and I have never thought about asking technical questions in quora
  • pramodc84
    pramodc84 over 5 years
    One tangent question, Can we have both Jersey and SpringRest in same EAR, both running together for different set of services?
  • malifa
    malifa over 3 years
    @pramodc84 : If the EAR has multiple .WAR files, then yes, 1st war can have jersey and 2nd war can use Srping MVC, 3rd war can use DropWizard, etc