HttpServlet类之间的混淆以及与Jersey一起使用它 [英] Confusion between HttpServlet class and using it with Jersey

查看:80
本文介绍了HttpServlet类之间的混淆以及与Jersey一起使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建实现​​RESTful API的servlet。我理解Jersey是一个解密和使用给定URL的框架。如何将它与 HttpServlet 类一起使用。

I am building servlets which implement a RESTful API. I understand the Jersey is a framework for deciphering and using given URL's. How do I use it in conjunction with the HttpServlet class.

我不明白两者如何相互配合。我想这是一个非常广泛的问题,但我已经做了相当多的阅读,但仍然坚持这个看似微不足道的概念。我试图使用扩展 HttpServlet 类的类来部署应用程序并使用Jersey注释。

I don't understand how the two work with each other. I guess this is a very broadstrokes question but I have done a fair share of reading but am still stuck on this seemingly trivial concept. I have attempted to deploy apps with classes that extend the HttpServletclass AND use Jersey annotations.

@Path("/api")
public class API extends HttpServlet{

@GET
@Path("/{name}")
@Produces("text/hmtl")
public String doGetSayHello(@PathParam("name") String name){
    return "Hello" + name;
}
@GET
@Path("/articles")
@Produces("text/json")
public String doGetArticles(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

    JSONObject obj = new JSONObject();
    obj.put("interns", interns);
    obj.put("company", "Stack Overflow");

    return obj.toString();

}

}

非常感谢任何帮助或信息材料!

Any help or informative materials would be greatly appreciated!

推荐答案

实际上你很困惑,因为你不明白球衣工作。 Jersey框架基本上使用 com.sun.jersey.spi.container.servlet.ServletContainer servlet来拦截所有传入的请求。正如我们在项目web.xml中配置的那样,所有传入的rest请求都应由该servlet处理。有一个使用jersey servlet配置的init-param来查找REST服务类。 REST服务类不是Servlet,它们不需要像在代码中那样扩展HttpServlet。这些REST服务类是简单的POJO注释,用于告诉jersey框架不同的属性,如路径,消耗,产生等。当您从服务方法返回时,jersey负责在定义的'PRODUCES'responseType中编组这些对象并编写它在客户端流上。以下是web.xml中的泽西配置示例

Actually you are confused because you don't understand how jersey works. Jersey framework basically uses com.sun.jersey.spi.container.servlet.ServletContainer servlet to intercept all the incoming requests. As we configure in our projects web.xml, that all the incoming rest request should be handled by that servlet. There is an init-param that is configured with the jersey servlet to find your REST service classes. REST service classes are not Servlet and they need NOT to extend the HttpServlet as you did in your code. These REST service classes are simple POJOs annotated to tell the jersey framework about different properties such as path, consumes, produces etc. When you return from your service method, jersey takes care of marshalling those objects in the defined 'PRODUCES' responseType and write it on the client stream. Here is a sample of jersey config in web.xml

<servlet>
        <servlet-name>REST</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>
                com.rest.services;
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>REST</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

这篇关于HttpServlet类之间的混淆以及与Jersey一起使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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