简体   繁体   English

Spring 4 vs Jersey用于REST Web服务

[英]Spring 4 vs Jersey for REST web services

We are planning to make a new application with spring 4.0.6 version. 我们计划用spring 4.0.6版本创建一个新的应用程序。 We use controller that can return "XML" or "JSON". 我们使用可以返回“XML”或“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. 在之前的项目中,我们使用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? 我仍然感到困惑,如果Spring证明了REST的非常好的支持那么为什么人们仍然会使用Jersey进行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!! 提前致谢!!

I'd say both Jersey and Spring MVC are great - each project has its own style and strengths. 我说Jersey和Spring MVC都很棒 - 每个项目都有自己的风格和优势。 Anyway, Stack Overflow is not the right place for asking subjective comparisons (your question would be closed quite quickly). 无论如何,Stack Overflow不适合进行主观比较(你的问题很快就会被关闭)。 If you're already using Spring for everything else and are not required to use JAX-RS, then Spring MVC makes total sense. 如果您已经将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使用HttpMessageConverters来处理(un)编组REST资源,而不是MessageBodyReaders / Writers。 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处理内容协商并为作业选择最佳可用转换器(您可以注释方法以暗示它们生成/使用的媒体类型)。

  2. No, it's not necessary to use JAX-RS to (un)marshall resources. 不,没有必要使用JAX-RS来(un)编组资源。 In fact, JAX-RS implementations and Spring MVC use third party serialization libraries to do the job; 事实上,JAX-RS实现和Spring MVC使用第三方序列化库来完成这项工作; 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. 在其4.0.6版本中,Spring支持许多HttpMessageConverters ,其中包括Jackson for JSON,JAXB for XML和许多其他版本。 Spring 4.1.0 added more HttpMessageConverters : Spring 4.1.0增加了更多HttpMessageConverters

To answer your last point, @XmlRootElement is a JAXB annotation and is not part of JAX-RS. 为了回答您的最后一点, @XmlRootElement是一个JAXB注释,不是JAX-RS的一部分。 Spring supports JAXB . 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等)。

AFAIK Spring REST support is based on Spring MVC and its not JAX-RS implementation while Jersey has implemented JAX-RS specification. AFAIK Spring REST支持基于Spring MVC而不是JAX-RS实现,而Jersey实现了JAX-RS规范。 Those having Spring (Core, AOP or MVC) in their project chooses Spring ReST support over JAX-RS implementor. 那些在他们的项目中使用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并且易于使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM