春季4与泽西(REST Web Services) [英] Spring 4 vs Jersey for REST web services

查看:110
本文介绍了春季4与泽西(REST Web Services)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们计划在Spring 4.0.6版本中创建一个新应用程序.我们使用可以返回"XML"或"JSON"的控制器. 在上一个项目中,我们已经使用JAX-RS API成功地将Jersey与Spring实现了REST支持,但是在阅读了前辈的几篇文章和建议后,他们说Spring提供了很好的REST支持.

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.

如果我不使用JAX-RS和Jersey而不使用Spring REST支持,则会使我真正感到困惑的一些观点是:

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

  1. 在Spring MVC中如何进行编组和拆组?

  1. How marshaling and unmarshaling done in Spring MVC ?

是否需要使用jax-rs进行编组或编组?

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

如果春季自动处理了编组和拆组,那么它如何知道xmlRootElements.

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

如果Spring证明对REST很好的支持,我仍然感到困惑,那为什么人们仍然选择Jersey来支持REST? 真的想了解更多细节.

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.

提前谢谢!

推荐答案

我会说Jersey和Spring MVC都很棒-每个项目都有自己的风格和优点.无论如何,Stack Overflow并不是进行主观比较的正确位置(您的问题很快就会被解决).如果您已经使用Spring进行其他所有操作,并且不需要使用JAX-RS,则Spring MVC完全有意义.

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.

关于(un)编组之类的功能,JAX-RS毕竟只是一个规范-其他库可以提供类似的功能而无需实现相同的API.

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. 而不是 MessageBodyReaders /作家,Spring MVC正在使用

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

否,没有必要使用JAX-RS来(取消)编组资源.实际上,JAX-RS实现和Spring MVC使用第三方序列化库来完成这项工作.因此它不受特定标准的约束.

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.

Spring在其4.0.6版本中支持许多 HttpMessageConverters :

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:

  • Jackson is now available for both JSON and XML
  • Google Protobuf
  • Gson for JSON, as an alternative to Jackson

要回答您的最后一点,@XmlRootElement是JAXB批注,不属于JAX-RS. 春天支持JAXB .

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

有关Spring中REST的更完整示例,请查看此入门指南(您将在10到15分钟的时间内得到一个完整的示例).

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

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

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

这篇关于春季4与泽西(REST Web Services)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆