需要用Java编写一个RESTful JSON服务
这是我的要求:
- 我在 mysql 中有一个简单的表(考虑任何带有几个字段的表),
- 我需要用 Java 编写一个简单的 RESTFUL JSON Web 服务,对该表执行 CRUD 操作。
我尝试在网上搜索一些全面的示例,但找不到任何示例。 有人可以帮忙吗?
Here is my requirement:
- I have a simple table in mysql(Consider any table with a few fields)
- I need to write a simple RESTFUL JSON webservice in Java that performs CRUD operations on this table.
I tried searching on the net for some comprehensive example for this but could not find any.
Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Jersey 是用于构建 RESTful Web 服务的 JAX-RS 实现。
从他们的教程开始。这很容易。
http://jersey.java.net/nonav/documentation/latest/getting-started.html< /a>
编辑: 另外,O'Riley 有一本关于这个主题的很棒的书(我知道,这很令人震惊);
使用 JAX-RS 的 RESTful Java
Jersey is a JAX-RS implementation for building RESTful webservices.
Start with their tutorial. It's pretty easy.
http://jersey.java.net/nonav/documentation/latest/getting-started.html
Edit: Also, there's a great O'Riley book on the subject (shocking, I know);
RESTful Java with JAX-RS
我会看看 Spring 提供了什么。有
RestTemplate
的,以及 春天MVC,两者都应该能够帮助你。另一件有用的事情是某种 JSON 映射库。我会推荐 Jackson 对象映射器。查看他们的教程,了解其工作原理。
I would take a look at what Spring has to offer. There are
RestTemplate
's, and Spring MVC, Both of which should be able to help you out.Another thing which will be helpful is some sort of JSON-Mapping library. I will recommend Jackson Object Mapper. Take a look at their tutorials for an idea of how it will work.
我将概述我的博客文章 构建 RESTful Web 服务的基本部分使用 Java 编写,其中显示了使用以下命令连接到数据库并创建 RESTful Web 服务可以采取的步骤。
以下描述假设您已经安装了上面列出的技术。该服务适用于数据库表“item”,该表存储带有字段 id、itemName、itemDescription、itemPrice 的项目。
持久层
http://localhost:4848
)。为了映射到数据库,使用了 JPA 实体。 JPA 实体是简单的带有 JPA 注释的 POJO(普通旧 Java 对象)。如果数据库已经存在,Eclipse 可以从数据库生成 JPA 实体。
使用 Eclipse 中的 SQL 剪贴簿使用以下 SQL 创建数据库表
通过右键单击在 Eclipse 中创建的包并选择从数据库表创建 JPA 实体新>表中的 JPA 实体。
由于此示例使用 UUID 作为主键,因此还有特定于 EclipseLink 的注释(@UuidGenerator 和 @GenerateValue)来负责创建它们。没有必要使用 UUID 作为主键,但我使用它的原因之一是,我可以在客户端上创建一个带有 UUID 的模型,然后将该新模型放入服务器(例如,在离线模式下,新模型)当单元信号返回时,本地创建和存储的模型将被 PUT 到服务器)。如果服务器创建了 UUID,则新模型将使用 POST 发送到没有 ID 的服务器。
创建文件夹 src\main\webapp\WEB-INF\classes\META-INF 并创建 persistence.xml 文件。
RESTful服务层
xml 片段
JAX -RS Service
创建定义基本 uri 的应用程序类。例如
http://localhost:8080/smallbiz/rest
从 Eclipse 内部署到 GlassFish。
尽管该示例使用 GlassFish,但任何兼容 Java EE 7 的容器都可以工作。如果您确实使用不同的容器(并且假设您对 JPA 使用相同的 EclipseLink,对 JAX-RS 实现使用 Jersey),您将必须:
希望它有帮助。
I will outline the essential parts of my blog post Building a RESTful Web Service in Java, which shows the steps you can take to connect to the database and create a RESTful Web Service using the following.
The following description assumes you have already installed the technology listed above. The service is for a database table 'item' that stores items with the fields id, itemName, itemDescription, itemPrice.
The Persistence Layer
http://localhost:4848
by default).To map to the database, a JPA Entity is used. A JPA Entity is simple a POJO (Plain Old Java Object) annotated with JPA annotations. If the database already exists, Eclipse can generate the JPA Entity from the database.
Create a database table using the SQL scrapbook in Eclipse with the following SQL
Create a JPA entity from the database table by right clicking a package created in Eclipse and selecting New > JPA Entities from Table.
Since this example uses UUID's for the primary key, there are also annotations (@UuidGenerator and @GeneratedValue) that are specific to EclipseLink to take care of creating them. It is not necessary to use UUID's as the primary keys but one of the reasons I use it is so that I can create a model on the client complete with a UUID and then PUT that new model to the server (eg in offline mode, new models created and stored locally are PUT to the server when cell signal returns). If the server created the UUID then new model is sent to the server without an id using POST.
Create folder src\main\webapp\WEB-INF\classes\META-INF and create a persistence.xml file.
The RESTful Service Layer
xml snippet
The JAX-RS Service
Create an Application Class that defines the base uri. eg for
http://localhost:8080/smallbiz/rest
Deploying to GlassFish from within Eclipse.
Though the example uses GlassFish, any Java EE 7 compliant container will work. If you do use a different container (and assuming you use the same EclipseLink for JPA and Jersey for JAX-RS implementations), you will have to:
Hope it is helpful.
这可能正是您正在寻找的:
http://restsql.org/doc/Overview.html
免责声明:
我从未使用过它 - 只是记得最近在新闻帖子中看到过它。
this might be doing exactly what you are looking for:
http://restsql.org/doc/Overview.html
DISCLAIMER:
I have never used it - just remembered seeing it recently in a news post.