使用JAX-RS混淆了JAX-RS和Jersey [英] Confusion with JAX-RS and Jersey with JAX-RS

查看:141
本文介绍了使用JAX-RS混淆了JAX-RS和Jersey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很困惑。我尝试了一个带有tomcat的Jax-rs并使用所有注释我能够使用 url 来调用我的服务。所以没有Jax-rs,我可以简单地拥有一个servlet并调用我的服务。另外,正如我所尝试的那样,jones-rs with jersey(因为我已经研究了它的 JAX-RS 的实现),并且在web.xml中如下。

I'm really confused of this. I have tried a Jax-rs with a tomcat and using all the annotations i was able to call my service using a url. So without Jax-rs I can simply have a servlet and call my service. Also as I have tried, there's jax-rs with jersey(As I have researched its a implementation of JAX-RS ) and in the web.xml theres following.

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>OutputUi</display-name>

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>
            com.sun.jersey.spi.container.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>org.xxx.carbon.servlet</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

然后我有一个与JAX-RS相同的注释,在GET上我可以调用我的使用正确的URL进行服务。

And then I have the annotation which is same as JAX-RS , on the GET I can call my service with correct URL.

我的问题是,为什么球衣使用servlet? JAX-RS 不使用servlet?为什么使用 JAX-RS ,而我们只能使用Servlet。

My question is, why jersey using a servlet? JAX-RS not using a servlet ? Why using JAX-RS, while we can use a just Servlet.

推荐答案

JAX-RS指定Servlets [ 1 ] 周围的部署模型。为什么,因为在Java世界中,这就是Web应用程序的运行方式。请求进入Servlet容器(如Tomcat或完整Java EE服务器中的容器),容器将请求移交给Servlet应用程序,应用程序处理请求并将响应吐出回容器,容器发送它给客户。 Spring MVC是一个基于Servlet的框架(主要的Servlet是 DispatcherServlet )。 JSF是一个基于Servlet的框架(主Servlet是 FacesServlet )。同样的方式JAX-RS是围绕Servlet构建的(主要的Servlet是特定于实现的;在Jersey的情况下,它是 ServletContainer )。

JAX-RS specifies a deployment model around Servlets[1]. Why, because in the Java world, that's how web applications are run. Request comes in to a Servlet container (like Tomcat or a container in a full Java EE server), the container hands off the request to the Servlet application, the application processes the request and spits out the response back to the container, which sends it to the client. Spring MVC is a Servlet based framework (main Servlet being DispatcherServlet). JSF is a Servlet based framework (main Servlet is FacesServlet). Same way JAX-RS is build around Servlets (main Servlet is implementation specific; in the case of Jersey it's ServletContainer).

请求进入Tomcat,它查找servlet映射,发现 ServletContainer 匹配请求URL,它将请求包装在 HttpServletRequest 中,并将其发送到Jersey Servlet。现在泽西岛可以随心所欲,这是一个很大的处理;例如处理请求以匹配您的方法 [ 2 ] ,反序列化实体主体,以及一大堆其他可以实现所有魔法的东西。当它完成处理后,它会将响应发送回容器。

Request comes in to Tomcat, it looks up the servlet mappings, it finds ServletContainer matches the request URL, it wraps the request in a HttpServletRequest and ships it off to the Jersey Servlet. Now Jersey can do with it as it pleases, which is a whole lot of processing; such as processing the request to match your methods[2], deserializing entity bodies, and a whole bunch of other stuff that makes all the magic possible. When it's done processing, it send the response back to the container.


为什么使用servlet的球衣?

why jersey using a servlet?

我认为上面已经说清楚了。

I think that is made clear above.


JAX-RS不使用一个servlet?

JAX-RS not using a servlet?

不确定我真的理解你的要求,但JAX-RS指定了其他部署模型,但是Servlet环境是唯一一个有任何具体要求细节的人。其他部署选项(如SE环境中)将是特定于实现的 [ 1 ]

Not really sure I really understand what you're asking by this, but JAX-RS specifies other deployment models, but a Servlet environment is the only one with any specific requirement details. Other deployment options, like in an SE environment will be implementation specific[1].


为什么使用JAX-RS,而我们只能使用Servlet

Why using JAX-RS, while we can use a just Servlet

你基本上会问,当我可以实现自己的REST框架时,为什么要使用JAX-RS?。通常,当有可用的框架时,请使用它。如果你觉得自己可以做得更好,那就去做吧。

You're basically asking, "Why should I use JAX-RS, when I can implement my own REST framework?". Generally, when there is a framework available, use it. If you feel you can make a better one, then do it.

[ 1 ] - 参见 2.3发布

[ 2 ] - 参见 3.7匹配资源方法请求

[1] - See 2.3 Publication
[2] - See 3.7 Matching Requests to Resource Methods

因此,部分OP的混淆是,他正在经历的教程中没有指定一个Servlet。 web.xml文件,它使OP认为正在使用vanilla JAX-RS(不存在),而不是实现。

So part of the confusion on part of the OP, was that the tutorial he was going through, didn't specify the a Servlet in a web.xml file, which made the OP think that a "vanilla JAX-RS" (which doesn't exist) was being used, ant not an implementation.

JAX -RS只是一个规范,没有实现就无法运行。是的,有一个 javax.ws.rx-api.jar 或一个 javaee-api.jar ,它有类/ interfaces / annotations to compile 一个JAX-RS应用程序,但这个jar中没有实际的引擎。实际的JAX-RS引擎在特定的实现jar中。

JAX-RS is just a specification, and cannot run without an implementation. Yes there is a javax.ws.rx-api.jar or a javaee-api.jar that has the classes/interfaces/annotations to compile a JAX-RS application, but there is no actual "engine" in this jar. The actual JAX-RS "engine" is in the specific implementation jars.

我没有看过完整的教程,但我认为它使用了上面的一个jar这导致OP相信没有使用JAX-RS实现。但实际上,所使用的Java EE服务器(也就是Glassfish)在内部都有实现。在Glassfish的情况下,它是泽西岛。

I haven't watched the complete tutorial, but I assume that it uses one of the above jars, which led the OP to believe that no JAX-RS implementation is used. But in actuality, the Java EE server used (which is Glassfish), internally has the implementation. In the case of Glassfish it's Jersey.

另一个混淆点可能出现在app配置中。而不是在OP的帖子中使用web.xml,而是使用了 Application 子类。类似

Another point of confusion may have been in the app configuration. Instead of using a web.xml as in the OP's post, there is an Application subclass being used. Something like

@ApplicationPath("/rest")
public class AppConfig extends Application {
    ...
}

JAX-RS规范说明当带有注释的这个类可用时,应使用上述完全限定的类名作为Servlet名称创建Servlet,并将URL映射为 @ApplicationPath 中的值。因此,无论您使用何种实现,此行为都应该相同。

The JAX-RS specification states that when this class with annotation is available, a Servlet should be created with the above fully qualified class name as the Servlet name, and the url mapping the be the value in the @ApplicationPath. So whatever implementation you are using, this behavior should be the same.

这篇关于使用JAX-RS混淆了JAX-RS和Jersey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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