不直接编写Servlet以创建REST API的原因 [英] Reasons for not directly writing Servlets for creating a REST API

查看:111
本文介绍了不直接编写Servlet以创建REST API的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我目前的公司中,我们正在开始一个新项目,该项目将是Java中的REST API,部署在像Tomcat这样的servlet容器中。在我之前使用REST框架(如JAX-RS和Jersey,JBOSS REST Easy,Spring MVC)的经验中,我知道使用类似于直接编写Servlet来处理请求的框架的一些优点。

In my current company we are starting a new project that will be a REST API in Java, deployed in a servlet container like Tomcat. In my previous experience using REST frameworks like JAX-RS with Jersey, JBOSS REST Easy, Spring MVC I know what are some of the advantages of using a framework like those over writing directly the Servlets for processing the requests.

(当然我们知道所提到的框架仍然使用Servlets)

(Of course we know that the mentioned frameworks still use Servlets under the covers)

我发现很难说服他们因为他们建议编写servlet,认为它对性能更好(可能是这种情况,但我认为使用其中一个框架的开销对REST API来说应该是微不足道的。)

I am finding difficult to convince them. As they are proposing to write servlets thinking it is better for performance (which can be the case but I think the overhead of using one of those frameworks should be insignificant for a REST API).

以下是我的理由:

1)更少的样板和更简洁的代码(更易于维护和测试)。使用JAX-RS框架或SpringMVC,您可以通过编写带有注释的方法来非常轻松地定义REST资源,注释指示资源的PATH,要使用的http方法,查询和url参数,接受编码的标题等等。

1) Less boilerplate and more concise code (which is easier to maintain and test). With a JAX-RS framework or SpringMVC you can define a REST resource very easily by writing methods with annotations indicating the PATH of the resource, the http method to use, query and url parameters, headers like encoding accepted, etc.

示例:

@GET
@Path("/users")
@Produces({MediaType.APPLICATION_JSON}) 
public UserList getUsers(@QueryParam("group") String group) {
    return userService.findUsers(group);
}

使用servlet至少需要这样的东西:

With servlets you will need at least something like this:

在web.xml中映射每个servlet的url(在Servlet 3.0中不需要):

Map the url for each servlet in web.xml (Which is not necessary in and above Servlet 3.0):

<servlet>
    <servlet-name>UsersServlet</servlet-name>
    <servlet-class>test.UsersServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>UsersServlet</servlet-name>
    <url-pattern>/users</url-pattern>
</servlet-mapping>

然后在servlet类中:

Then inside the servlet class:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    String group = request.getParameter("group");
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    JsonSerializer someJsonSerializer = new JsonSerializer();
    String json = someJsonSerializer.serialize(userService.findUsers(group));      
    out.print(json);
}

2)适应性。上述框架允许您轻松地向应用程序添加功能,否则您将需要手动执行,例如使用多个媒体类型输入和输出。例如,根据accept标头,使服务返回xml或json或任何其他服务。像SpringMVC和Jersey这样的框架可以很容易地为您的请求和响应配置序列化器/反序列化器。

2) Adaptability. The mentioned frameworks allow you to easily add features to your application that otherwise you will need to do it manually, like using multiple media type inputs and outputs. For example making a service to return xml or json or any other depending on the accept header. Frameworks like SpringMVC and Jersey make it very easy to configure serializers/deserializers for your requests, responses.

3) REST最佳实践。通常,这些框架是基于对REST API遵循的最佳实践的充分理解而构建的,并且基于REST体系结构的标准进行定义,这使得构建可靠且标准的符合标准的应用程序变得更加容易。另一方面,Servlets为您提供了如何处理请求/响应的高度自由,以至于您很难意识到您根本不是RESTfull。

3) REST best practices. Normally those frameworks are built over a solid understanding of the best practices to be followed by a REST API and are defined based on standards of the REST architecture which makes easier to build a solid and standard conforming application. In the other hand Servlets give you a so high level of freedom on how to process your requests/responses that it will be more difficult to realize that you are not being RESTfull at all.

任何其他?

推荐答案

让我用我的答案扮演魔鬼的拥护者。

Let me play the devil's advocate with my answer.

首先,您不需要将servlet添加到web.xml文件中。 Servlets 3.0允许您使用注释

First, you don't need to add the servlets to the web.xml file. Servlets 3.0 allow you to use annotations.

其次,这些框架确实对性能产生了重大影响。请参阅这些基准

Second, there really is a significant performance hit with these frameworks. See these benchmarks

第三,你可以在servlet中使用 GSON ,这比Jackson更快(默认情况下在Spring中使用)泽西)。这可以让您获得更高的性能,特别是考虑到性能对您的要求至关重要。

Third, you can use GSON within a servlet, which is faster than Jackson (used by default in Spring and Jersey). This gets you even more performance especially considering that performance is critical to your requirements.

最后,如果您关注样板文件,请将您在servlet中编写的代码放入一些实用程序类,并从多个servlet中使用它。当你(像大多数人一样)可能会使用它的一小部分功能时,这会超过框架的巨大负荷。

Finally, if you are concerned about boilerplate, put that code that you wrote inside the servlet in some utility class and use it from multiple servlets. That beats carrying around a framework's huge load when you (like most people) would probably be using a small fraction of its functionality.

这篇关于不直接编写Servlet以创建REST API的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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