Spring 4 vs Jersey用于REST Web服务

时间:2021-06-06 19:33:38

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.

我们计划用spring 4.0.6版本创建一个新的应用程序。我们使用可以返回“XML”或“JSON”的控制器。在之前的项目中,我们使用JAX-RS API成功实现了Jersey with Spring for REST支持,但在阅读了一些文章和老年人的建议后,他们说Spring提供了非常好的REST支持。

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

如果我在不使用JAX-RS和Jersey的情况下使用Spring REST支持,那些让我感到困惑的一些要点是:

  1. How marshaling and unmarshaling done in Spring MVC ?

    如何在Spring MVC中完成编组和解组?

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

    编组或解组是否需要使用jax-rs。

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

    如果弹簧自动处理编组和解编,那么它如何知道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.

我仍然感到困惑,如果Spring证明了对REST的非常好的支持那么为什么人们仍然会使用Jersey进行REST?真的希望了解更多细节。

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

如果我说错了,请忽略它。对示例的解释非常有用。

Thanks in advance!!

提前致谢!!

2 个解决方案

#1


110  

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.

我会说Jersey和Spring MVC都很棒 - 每个项目都有自己的风格和优势。无论如何,Stack Overflow不适合进行主观比较(你的问题很快就会被关闭)。如果你已经将Spring用于其他所有事情并且不需要使用JAX-RS,那么Spring MVC就足够了。

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

关于(un)编组等功能,JAX-RS毕竟只是一个规范 - 其他库可以提供类似的功能而无需实现相同的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).

    Spring MVC使用HttpMessageConverters来处理(un)编组REST资源,而不是MessageBodyReaders / Writers。 Spring MVC处理内容协商并为作业选择最佳可用转换器(您可以注释方法以暗示它们生成/使用的媒体类型)。

  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.

    不,没有必要使用JAX-RS来(un)编组资源。实际上,JAX-RS实现和Spring MVC使用第三方序列化库来完成工作;所以它不依赖于特定的标准。

  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:

    在其4.0.6版本中,Spring支持许多HttpMessageConverters,其中包括Jackson for JSON,JAXB for XML和许多其他版本。 Spring 4.1.0增加了更多HttpMessageConverters:

    • Jackson is now available for both JSON and XML
    • Jackson现在可用于JSON和XML
    • Google Protobuf
    • Google Protobuf
    • Gson for JSON, as an alternative to Jackson
    • Gson为JSON,作为杰克逊的替代品

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

为了回答您的最后一点,@ XmlRootElement是一个JAXB注释,不是JAX-RS的一部分。 Spring支持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).

有关Spring中REST的更完整示例,请查看此入门指南(您将在10-15分钟内获得完整的示例)。

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).

你问题的最后一部分是非常主观的 - 在JVM中有许多流行的解决方案用于构建REST服务,而不仅仅是Jersey和Spring(Dropwizard,Play!Framework等)。

#2


22  

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.

AFAIK Spring REST支持基于Spring MVC而不是JAX-RS实现,而Jersey实现了JAX-RS规范。那些在项目中使用Spring(Core,AOP或MVC)的人选择Spring ReST支持而不是JAX-RS实现者。

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

我推荐Jersey成熟,实现JAX-RS并且易于使用。

#1


110  

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.

我会说Jersey和Spring MVC都很棒 - 每个项目都有自己的风格和优势。无论如何,Stack Overflow不适合进行主观比较(你的问题很快就会被关闭)。如果你已经将Spring用于其他所有事情并且不需要使用JAX-RS,那么Spring MVC就足够了。

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

关于(un)编组等功能,JAX-RS毕竟只是一个规范 - 其他库可以提供类似的功能而无需实现相同的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).

    Spring MVC使用HttpMessageConverters来处理(un)编组REST资源,而不是MessageBodyReaders / Writers。 Spring MVC处理内容协商并为作业选择最佳可用转换器(您可以注释方法以暗示它们生成/使用的媒体类型)。

  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.

    不,没有必要使用JAX-RS来(un)编组资源。实际上,JAX-RS实现和Spring MVC使用第三方序列化库来完成工作;所以它不依赖于特定的标准。

  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:

    在其4.0.6版本中,Spring支持许多HttpMessageConverters,其中包括Jackson for JSON,JAXB for XML和许多其他版本。 Spring 4.1.0增加了更多HttpMessageConverters:

    • Jackson is now available for both JSON and XML
    • Jackson现在可用于JSON和XML
    • Google Protobuf
    • Google Protobuf
    • Gson for JSON, as an alternative to Jackson
    • Gson为JSON,作为杰克逊的替代品

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

为了回答您的最后一点,@ XmlRootElement是一个JAXB注释,不是JAX-RS的一部分。 Spring支持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).

有关Spring中REST的更完整示例,请查看此入门指南(您将在10-15分钟内获得完整的示例)。

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).

你问题的最后一部分是非常主观的 - 在JVM中有许多流行的解决方案用于构建REST服务,而不仅仅是Jersey和Spring(Dropwizard,Play!Framework等)。

#2


22  

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.

AFAIK Spring REST支持基于Spring MVC而不是JAX-RS实现,而Jersey实现了JAX-RS规范。那些在项目中使用Spring(Core,AOP或MVC)的人选择Spring ReST支持而不是JAX-RS实现者。

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

我推荐Jersey成熟,实现JAX-RS并且易于使用。