Servlet 与 RESTful

发布于 2024-12-11 10:52:58 字数 150 浏览 0 评论 0原文

今天我读到了 Restful 服务。基本上我的理解是 Restful Web 服务将在 HTTP 请求方法上工作,而不是普通的 Web 服务将在 SOAP 请求上工​​作。

既然普通的 servlet 也可以在 HTTP 方法上工作,那么 Restful 服务需要什么?

Today I read about Restful services. Basically what I understand that is Restful webservices will work on HTTP request methods rather than normal webservice will work on SOAP request.

What is the need for Restful services as normal servlet can also work on the HTTP methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

草莓味的萝莉 2024-12-18 10:52:58

RESTful 与其说是一种不同的技术,不如说是一种架构风格。从服务器的角度来看,它被设计为完全无状态并且基于每个请求是独立的(即没有会话)。从客户端的角度来看,它更像是一种通过带有(自文档化)路径参数而不是请求参数的 URL 获取不同格式信息的方法。

当然,您可以使用普通的 servlet 来完成此操作,但它会引入一些样板代码来收集路径参数并生成所需的响应。 JAX-RS 只是一个方便且独立的 API,它消除了您自己编写所有样板代码的需要,从而产生最少且更多的自文档化代码。

假设您有一个如下所示的 JAXB 实体模型:

@XmlRootElement
public class Data {

    @XmlElement
    private Long id;

    @XmlElement
    private String value;

    // ...

    @Override
    public String toString() {
        return String.format("Data[id=%d,value=%s]", id, value);
    }

}

以及如下所示的 JAX-RS 资源:

@Path("data")
public class DataResource {

    @EJB
    private DataService service;

    @GET 
    @Path("text/{id}")
    @Produces(MediaType.TEXT_PLAIN)
    public String getAsText(@PathParam("id") Long id) {
        return String.valueOf(service.find(id));
    }

    @GET 
    @Path("xml/{id}")
    @Produces(MediaType.APPLICATION_XML)
    public Data getAsXml(@PathParam("id") Long id) {
        return service.find(id);
    }

    @GET 
    @Path("json/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Data getAsJson(@PathParam("id") Long id) {
        return service.find(id);
    }

}

那么您已经通过以下方式获得了正确格式的所需内容:

就是这样。尝试对单个普通 Servlet 执行相同的操作:) 请注意,SOAP 本质上通过 HTTP 进行。它基本上是 HTTP 上的额外 XML 层,而不是不同的网络协议。

另请参阅:

RESTful is more an architecture style than a different technology. In server perspective, it is designed to be entirely stateless and self-contained on a per-request basis (i.e. there are no sessions). In client perspective, it's more a way of getting information in different formats via URLs with (self-documenting) path parameters instead of request parameters.

Surely you can do this with a plain vanilla servlet, but it would introduce some boilerplate code to gather the path parameters and to generate the desired response. JAX-RS is just a convenient and self-containing API which removes the need for writing all the boilerplate code yourself, resulting in minimal and more self-documenting code.

Assuming that you've a JAXB entity as model as below:

@XmlRootElement
public class Data {

    @XmlElement
    private Long id;

    @XmlElement
    private String value;

    // ...

    @Override
    public String toString() {
        return String.format("Data[id=%d,value=%s]", id, value);
    }

}

And a JAX-RS resource as below:

@Path("data")
public class DataResource {

    @EJB
    private DataService service;

    @GET 
    @Path("text/{id}")
    @Produces(MediaType.TEXT_PLAIN)
    public String getAsText(@PathParam("id") Long id) {
        return String.valueOf(service.find(id));
    }

    @GET 
    @Path("xml/{id}")
    @Produces(MediaType.APPLICATION_XML)
    public Data getAsXml(@PathParam("id") Long id) {
        return service.find(id);
    }

    @GET 
    @Path("json/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Data getAsJson(@PathParam("id") Long id) {
        return service.find(id);
    }

}

Then you'd already get the desired content in proper format by:

That's it. Try to do the same with a single plain vanilla Servlet :) Please note that SOAP essentially also goes over HTTP. It's basically an extra XML layer over HTTP, not a different network protocol.

See also:

瘫痪情歌 2024-12-18 10:52:58

在我看来,为了更好地理解,我们需要剖析那些让我们困惑的组件,这些组件是,

  1. REST 概念

Fielding 使用 REST 设计 HTTP 1.1 和统一资源标识符
(URI)

  1. HTTP 协议 -
    超文本传输​​协议
  2. javax.servlet.http.HttpServlet class
  3. 使用 Java 实现 REST - JAX-RS 及其实现(如 Jersey 等)
  4. 不符合 JAX-RS 的其他 REST 实现(如 Spring REST )
    JAX-RS 和 Spring Rest 之间的区别

那么如果您参考 这个答案了解这些实现方式使用Servlet(具体的javax.servlet.http.HttpServlet)来拦截所有传入请求。有重要的引述,

这些 REST 服务类是简单的 POJO,带有注释来告诉
关于不同属性的球衣框架,例如路径、消耗、
生产等

你可以进一步阅读 - REST 和 REST 有什么区别和 HTTP 协议? & HTTP 和 REST 之间有什么区别? 并制作一个结论是,如果您将 Web 服务设为 RESTFul,您将获得哪些优势,即(从一个答案复制),

REST 不一定与 HTTP 相关。 RESTful Web 服务只是遵循 RESTful 架构的 Web 服务。

What is Rest -
1- Client-server
2- Stateless
3- Cacheable
4- Layered system
5- Code on demand
6- Uniform interface

使用 REST 的优势是什么

不过,我不想陷入优缺点(优点和缺点)的争论,因为这是非常主观的。

读完了上面的内容,现在回答你的问题,

普通servlet也可以工作,需要Restful服务吗
关于 HTTP 方法?

您会明白,REST 框架只是简化了企业级 REST 服务的实现,但它们确实使用 HTTP Servlet 来拦截传入请求。您始终可以使用普通 servlet 来实现您自己的 REST 服务,但如果使用大量样板代码,这只会更加耗时。

In my opinion, for a better understanding , we need to dissect components that confuse us and these components are ,

  1. REST Concept

Fielding used REST to design HTTP 1.1 and Uniform Resource Identifiers
(URI)

  1. HTTP Protocol -
    Hypertext Transfer Protocol
  2. javax.servlet.http.HttpServlet class
  3. REST with Java - JAX-RS and its implementations ( like Jersey etc)
  4. Other REST Implementations not conforming to JAX-RS ( like Spring REST )
    Difference between JAX-RS and Spring Rest

Then if you refer this answer to understand as how these implementation use a Servlet ( A concrete javax.servlet.http.HttpServlet ) to intercept all incoming requests. Important quote there is,

These REST service classes are simple POJOs annotated to tell the
jersey framework about different properties such as path, consumes,
produces etc.

Then you can further read about - What is the difference between REST and HTTP protocols? & What is the difference between HTTP and REST? and make a conclusion as what advantages you get if you make your web service RESTFul , namely ( copied from one answer ) ,

REST is not necessarily tied to HTTP. RESTful web services are just web services that follow a RESTful architecture.

What is Rest -
1- Client-server
2- Stateless
3- Cacheable
4- Layered system
5- Code on demand
6- Uniform interface

What is the advantage of using REST instead of non-REST HTTP?

Though, I wouldn't like to get into advantages - disadvantages ( pros & cons ) debate as that is very subjective.

With above readings , now for your question ,

What is the need for Restful services as normal servlet can also work
on the HTTP methods?

You would understand that REST Frameworks simply simplify implementation of REST Services at enterprise level but they do use HTTP Servlet to intercept incoming requests. You can always use plain servlets to implement your own REST services but that would simply be more time consuming with lots of boiler plate code.

旧街凉风 2024-12-18 10:52:58
  • RESTeasy 有一个专有的 @Form,它将表单参数(例如 HTTP 正文中的 [BuildVersion]&[BuildVersion]...)绑定到 Java 对象。如果您正在处理表单内容,您将节省大量精力将 @formparam 绑定到 Java 对象
  • RESTeasy 的 URL 或查询专有缓存

RESTeasy 更容易进行 EJB 3.0 和 SEAM 集成,而 Jersey 更容易进行 Spring 和 JSON 集成.

  • RESTeasy has a proprietary @Form that binds form parameters (e.g. [BuildVersion]&[BuildVersion]... in the HTTP body) to Java objects. If you are handling form contents, you will save considerable efforts to bind the @formparam to Java objects
  • RESTeasy's proprietary cache for the URL or query

RESTeasy is easier for EJB 3.0 and SEAM integration, whereas Jersey is easier for Spring and JSON integration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文