HttpServlet 类和将其与 Jersey 一起使用之间的混淆

Confusion between HttpServlet class and using it with Jersey(HttpServlet 类和将其与 Jersey 一起使用之间的混淆)

本文介绍了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 中配置的那样,所有传入的休息请求都应由该 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 一起使用之间的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:HttpServlet 类和将其与 Jersey 一起使用之间的混淆

基础教程推荐