Java Spring 4 与 Jersey 的 REST Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26305363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring 4 vs Jersey for REST web services
提问by Robin Saxena
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 和 Spring 的 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 支持,那么让我感到困惑的一些要点是:
How marshaling and unmarshaling done in Spring MVC ?
Is it necessary for marshaling or unmarshaling need to use jax-rs.
If marshaling and unmarshaling are automatically handled by spring then how does it know about xmlRootElements.
如何在 Spring MVC 中进行编组和解组?
是否需要使用 jax-rs 进行编组或解组。
如果编组和解组是由 spring 自动处理的,那么它如何知道 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!!
提前致谢!!
采纳答案by Brian Clozel
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 requiredto 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) marshalling 之类的功能,JAX-RS 毕竟只是一个规范——其他库可以提供类似的功能,而无需实现相同的 API。
Instead of MessageBodyReaders/Writers, Spring MVC is using HttpMessageConvertersto 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).
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.
In its 4.0.6 version, Spring supports many HttpMessageConverters, with Hymanson for JSON, JAXB for XML and many others. Spring 4.1.0 added more HttpMessageConverters:
- Hymanson is now available for both JSON and XML
- Google Protobuf
- Gson for JSON, as an alternative to Hymanson
Spring MVC 使用HttpMessageConverters来处理(取消)编组 REST 资源,而不是MessageBodyReaders/Writers 。Spring MVC 处理内容协商并为作业选择最佳可用转换器(您可以注释方法以提示它们生产/消费的媒体类型)。
不,没有必要使用 JAX-RS 来(取消)编组资源。事实上,JAX-RS 实现和 Spring MVC 使用第三方序列化库来完成这项工作;所以它不依赖于特定的标准。
在 4.0.6 版本中,Spring 支持许多HttpMessageConverters,Hymanson 用于 JSON,JAXB 用于 XML 等等。Spring 4.1.0 添加了更多HttpMessageConverters:
- Hymanson 现在可用于 JSON 和 XML
- 谷歌原型缓冲区
- Gson for JSON,作为 Hymanson 的替代品
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 等)。
回答by VGaur
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 并且易于使用。