简体   繁体   English

从JSP / Servlet应用程序公开REST接口

[英]Exposing REST interface from a JSP/Servlet Application

I have created a simple Web Portal using JSP/Servlets. 我已经使用JSP / Servlet创建了一个简单的Web门户。 It is connected to MySQL Database and basically the portal is just used to manage the records in the MySQL Database. 它连接到MySQL数据库,并且基本上门户仅用于管理MySQL数据库中的记录。

I now want to expose REST APIs for anyone to retrieve and insert information into the MySQL database. 现在,我想向所有人公开REST API,以供所有人检索信息并将其插入MySQL数据库。 Would I be able to do so using JSP/Servlets. 我可以使用JSP / Servlet执行此操作。

It would be great to have suggestions. 提出建议真是太好了。

Lets first get some short definitions: 首先让我们得到一些简短的定义:

REST is an architecture design. REST是一种体系结构设计。 You can read this clear explanation from Wikipedia . 您可以从Wikipedia阅读此清晰的说明。 Today most REST services use JSON as a message format, but this is not mandatory neighter part of REST. 今天,大多数REST服务都使用JSON作为消息格式,但这并不是REST必不可少的部分。 You can have XML, images, etc. 您可以具有XML,图像等。

Servlet is a RESTful implementation. Servlet是RESTful实现。 You can extend HttpServlet in Java an implement the RESTful methods (GET, PUT, POST, DELETE). 您可以在Java中扩展HttpServlet并实现RESTful方法(GET,PUT,POST,DELETE)。 Lot of people just go with POST and GET, so it is not RESTful by definition, but it do the work. 很多人只是使用POST和GET,因此从定义上讲它不是RESTful的,但是可以工作。

JSP is not what you are looking for in this case. 在这种情况下, JSP不是您想要的。 Its mostly designed for user interface, and is not the right approach to expose "API"s as services and such. 它主要是为用户界面设计的,不是将“ API”公开为服务等的正确方法。

Now about your question, the answer is yes you can use Servlets, but there are better tools to implement REST today. 现在关于您的问题,答案是可以使用Servlet,但是今天有更好的工具来实现REST。 You'll have a litte more work to set up your structure but it will be an elegant solution. 您将需要更多工作来设置结构,但这将是一个很好的解决方案。

The way to go with Java is with JAX-RS , the Java Enterprise specification for RESTful Services. 使用Java的方法是使用JAX-RS (RESTful服务的Java Enterprise规范)。 You can follow the official tutorial Building RESTful Web Services with JAX-RS . 您可以遵循官方教程“ 使用JAX-RS构建RESTful Web服务” I highly recommend you to follow this tutorial since it will give you a good grasp over the concepts involved. 我强烈建议您遵循本教程,因为它将使您对所涉及的概念有很好的了解。

You can use Spring too: Building a RESTful Web Service . 您也可以使用Spring构建RESTful Web服务 I use Jackson 2.0 with Spring and it works like a charm. 我在Spring上使用Jackson 2.0,它的工作原理很吸引人。

Other example here . 这里的其他例子。

Yes, you can do this only with servlets, but this is not a great idea, because there are a lot of good frameworks to do this. 是的,您只能使用servlet来执行此操作,但这不是一个好主意,因为有很多好的框架可以执行此操作。

Take a look a this link Jersey 看看一个这个链接球衣

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM