需要用Java编写一个RESTful JSON服务

发布于 2024-12-07 07:49:43 字数 173 浏览 0 评论 0原文

这是我的要求:

  1. 我在 mysql 中有一个简单的表(考虑任何带有几个字段的表),
  2. 我需要用 Java 编写一个简单的 RESTFUL JSON Web 服务,对该表执行 CRUD 操作。

我尝试在网上搜索一些全面的示例,但找不到任何示例。 有人可以帮忙吗?

Here is my requirement:

  1. I have a simple table in mysql(Consider any table with a few fields)
  2. 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 技术交流群。

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

发布评论

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

评论(4

柏拉图鍀咏恒 2024-12-14 07:49:43

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

寄离 2024-12-14 07:49:43

我会看看 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.

好多鱼好多余 2024-12-14 07:49:43

我将概述我的博客文章 构建 RESTful Web 服务的基本部分使用 Java 编写,其中显示了使用以下命令连接到数据库并创建 RESTful Web 服务可以采取的步骤。

  • IDE:适用于 Jave EE 开发人员的 Eclipse IDE (Kepler),附带内置 Maven
  • 数据库:MySQL(也使用 MySQL Workbench)
  • 应用程序服务器:GlassFish 4.0
  • Java EE 7(JAX-RS、JPA、JAXB 等)
  • 任何 REST 客户端用于测试:(例如Postman)

以下描述假设您已经安装了上面列出的技术。该服务适用于数据库表“item”,该表存储带有字段 id、itemName、itemDescription、itemPrice 的项目。

持久层

  • 使用 MySQL Workbench 的“在连接的服务器中创建新架构”图标创建 MySQL 数据库架构。在此示例中,模式称为“smallbiz”。
  • 将 Eclipse 连接到 MySQL
    • 下载 MySQL JDBC 驱动程序并将其保存在方便的位置。
    • 在 Eclipse 中,在数据源资源管理器选项卡中创建一个新的数据库连接。在此过程中,您将需要浏览到 MySQL JDBC 驱动程序。
  • 在 GlassFish 中创建 MySQL JDBC 连接池和资源(这是应用程序部署到服务器后能够连接数据库所必需的)。
    • 将 MySQL JDBC 驱动程序复制到 GlassFish 中的 ext 目录,即 $glassfish_install_folder\glassfish\lib\ext。然后重新启动 GlassFish。
    • 使用浏览器登录 GlassFish 管理区域(默认位置为 http://localhost:4848)。
    • 资源>下JDBC>连接池,创建一个“资源类型”为 java.sql.Driver 的新连接池。
    • 在“新建 JDBC 连接池(第 1 步,共 2 步)”屏幕上。
      • 输入“池名称”(例如 SmallBizPool)。
      • 选择“资源类型”(java.sql.Driver)。
      • 选择“数据库驱动程序供应商”(MySQL)。
      • 下一步
    • 位于以下屏幕底部的“其他属性”中。
      • URL: jdbc:mysql://localhost:3306/smallbiz
      • 用户: yourUser(我的设置中的根用户)
      • 密码:您的密码
      • 完成
      • 在以下屏幕上Ping以测试连接。
    • Resources > 处的连接池创建 JDBC 资源JDBC> JDBC 资源
      • 输入“JNDI 名称”(例如 jdbc/SmallBiz)。我们在 persistence.xml 文件中使用它,以便应用程序知道如何连接到数据库。
      • 从“池名称”下拉列表中选择之前创建的连接池。
    • 有关 JDBC 连接池和资源的更多信息
  • 将 Eclipse 连接到 GlassFish。
    • 通过搜索帮助> 将 GlassFish 工具添加到 Eclipse Eclipse 市场
    • 通过使用“服务器”选项卡创建新服务器,将 GlassFish 连接到 Eclipse。
      • 在“定义新服务器”屏幕上选择 GlassFish 4.0。
      • 确保我在New GlasssFish 4.0 Runtime屏幕中选择了“jdk7”和JDK
      • 在接下来的屏幕上,确保选择正确的域目录(默认为domain1,例如$glassfish_install_folder\glassfish\domains\domain1),然后输入GlassFish的管理员凭据。< /里>
      • 您现在可以在 Eclipse 中与 GlassFish 交互(启动、停止、重新启动、运行等)。
  • 在 Eclipse 中创建一个新的 Maven 项目,并使用 m2e(内置)添加依赖项:
    • EclipseLink(用于 JPA)“org.eclipse.persistence eclipselink”

为了映射到数据库,使用了 JPA 实体。 JPA 实体是简单的带有 JPA 注释的 POJO(普通旧 Java 对象)。如果数据库已经存在,Eclipse 可以从数据库生成 JPA 实体。

  • 使用 Eclipse 中的 SQL 剪贴簿使用以下 SQL 创建数据库表

    创建表项(
         id VARCHAR(36) NOT NULL,
         itemName 文本不为空,
         项目描述文本,
         商品价格双倍,
         主键(id)
     )
    
  • 通过右键单击在 Eclipse 中创建的包并选择从数据库表创建 JPA 实体新>表中的 JPA 实体

  • 使用 JAXB 注释来注释 JPA 实体,以便能够将其编组到 xml 或 json 或从 xml 或 json 编组。

由于此示例使用 UUID 作为主键,因此还有特定于 EclipseLink 的注释(@UuidGenerator 和 @GenerateValue)来负责创建它们。没有必要使用 UUID 作为主键,但我使用它的原因之一是,我可以在客户端上创建一个带有 UUID 的模型,然后将该新模型放入服务器(例如,在离线模式下,新模型)当单元信号返回时,本地创建和存储的模型将被 PUT 到服务器)。如果服务器创建了 UUID,则新模型将使用 POST 发送到没有 ID 的服务器。

package com.zangolie.smallbiz.entities;

import java.io.Serializable;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.annotations.UuidGenerator;


/**
 * The persistent class for the item database table.
 * 
 */
@UuidGenerator(name="UUID")
@XmlRootElement
@Entity
@NamedQuery(name="Item.findAll", query="SELECT i FROM Item i")
public class Item implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator="UUID")
    @Column(length=36)
    private String id;

    private String itemDescription;

    @Lob
    private String itemName;

    private double itemPrice;

    public Item() {
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getItemDescription() {
        return this.itemDescription;
    }

    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }

    public String getItemName() {
        return this.itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public double getItemPrice() {
        return this.itemPrice;
    }

    public void setItemPrice(double itemPrice) {
        this.itemPrice = itemPrice;
    }

}
  • 创建文件夹 src\main\webapp\WEB-INF\classes\META-INF 并创建 persistence.xml 文件。

    
    
    <持久化版本=“2.1”
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
        <持久性单元名称=“testPU”事务类型=“JTA”>
            jdbc/SmallBiz
        
    
    

RESTful服务层

  • 将Maven编译器设置为1.7
    • 右键单击 pom.xml 文件并选择“打开方式”> Maven POM 编辑器。在结束标记之前添加以下内容。

xml 片段

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • 使用 m2e 添加依赖项:
    • Servlet 3.1(JAX-RS 2.0 需要此)“javax.servlet javax.servlet.api”
    • Jersey(用于 JAX-RS)“org.glassfish.jersey.core jersey-sever”
    • EJB(需要使用@Stateless注释)“javax.ejb javax.ejb.api”
  • 创建 POJO(普通旧 java 对象)并使用 JAX-RS 注释对其进行注释创建端点。
    • @Path("/your-app-api-uri"):指示资源所在的相对于服务器基本 URI 的路径
    • @Produces ({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}):生成的表示形式可以是 json 或 xml。
    • @Consumes ({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}):使用的表示形式可以是 json 或 xml
    • @Stateless:服务器不存储交互之间的状态(这是 RESTful 架构的原则之一)。
  • 注释映射到 CRUD 操作的方法
    • @POST:创建
    • @GET:阅读
    • @PUT:更新
    • @DELETE:删除

JAX -RS Service

package com.zangolie.smallbiz.services.rest;

import java.net.URI;
import java.util.Collection;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import com.zangolie.smallbiz.entities.Item;

@Path("/item")
@Produces ({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Consumes ({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Stateless
public class ItemRestService {
    //the PersistenceContext annotation is a shortcut that hides the fact
    //that, an entity manager is always obtained from an EntityManagerFactory.
    //The peristitence.xml file defines persistence units which is supplied by name
    //to the EntityManagerFactory, thus  dictating settings and classes used by the
    //entity manager
    @PersistenceContext(unitName = "testPU")
    private EntityManager em;

    //Inject UriInfo to build the uri used in the POST response
    @Context
    private UriInfo uriInfo;

    @POST
    public Response createItem(Item item){
        if(item == null){
            throw new BadRequestException();
        }
        em.persist(item);

        //Build a uri with the Item id appended to the absolute path
        //This is so the client gets the Item id and also has the path to the resource created
        URI itemUri = uriInfo.getAbsolutePathBuilder().path(item.getId()).build();

        //The created response will not have a body. The itemUri will be in the Header
        return Response.created(itemUri).build();
    }

    @GET
    @Path("{id}")
    public Response getItem(@PathParam("id") String id){
        Item item = em.find(Item.class, id);

        if(item == null){
            throw new NotFoundException();
        }

        return Response.ok(item).build();
    }

    //Response.ok() does not accept collections
    //But we return a collection and JAX-RS will generate header 200 OK and
    //will handle converting the collection to xml or json as the body
    @GET
    public Collection<Item> getItems(){
        TypedQuery<Item> query = em.createNamedQuery("Item.findAll", Item.class);
        return query.getResultList();
    }

    @PUT
    @Path("{id}")
    public Response updateItem(Item item, @PathParam("id") String id){
        if(id == null){
            throw new BadRequestException();
        }

        //Ideally we should check the id is a valid UUID. Not implementing for now
        item.setId(id);
        em.merge(item);

        return Response.ok().build();
    }

    @DELETE
    @Path("{id}")
    public Response deleteItem(@PathParam("id") String id){
        Item item = em.find(Item.class, id);
        if(item == null){
            throw new NotFoundException();
        }
        em.remove(item);
        return Response.noContent().build();
    }

}
  • 创建定义基本 uri 的应用程序类。例如 http://localhost:8080/smallbiz/rest

    包 com.zangolie.smallbiz.services.rest;
    
    导入 javax.ws.rs.ApplicationPath;
    导入javax.ws.rs.core.Application;
    
    @ApplicationPath(“休息”)
    公共类ApplicationConfig扩展应用程序{
    
    }
    
  • 从 Eclipse 内部署到 GlassFish。

    • 右键单击项目运行方式>在服务器上运行
    • 选择已连接的 GlassFish 服务器。
  • 使用任何 HTTP 客户端进行测试(例如在 chrome 中工作的 Postman)。

尽管该示例使用 GlassFish,但任何兼容 Java EE 7 的容器都可以工作。如果您确实使用不同的容器(并且假设您对 JPA 使用相同的 EclipseLink,对 JAX-RS 实现使用 Jersey),您将必须:

  • 弄清楚如何从 Eclipse 连接到它。
  • 将 Jersey 和 EclipseLink 的 Maven 依赖项从提供更改为编译(因为这些是 GlassFish 中的实现,在其他容器中可能有所不同)。

希望它有帮助。

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.

  • IDE: Eclipse IDE for Jave EE Developers (Kepler), comes with Maven built in
  • Database: MySQL (also makes use of MySQL Workbench)
  • Application Server: GlassFish 4.0
  • Java EE 7 (JAX-RS, JPA, JAXB, etc)
  • Any REST Client for testing: (eg Postman)

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

  • Create a MySQL database schema using MySQL Workbench's ‘create a new schema in connected server‘ icon. In this example the schema is called 'smallbiz'.
  • Connect Eclipse to MySQL
    • Download the MySQL JDBC driver and save in a convenient location.
    • In Eclipse, create a new Database Connection in the Data Source Explorer tab. During this process you will need to browse to the MySQL JDBC driver.
  • Create a MySQL JDBC Connection Pool and Resource in GlassFish (this is required for the application to be able to connect the database once deployed on the server).
    • Copy the MySQL JDBC driver to the ext directory in GlassFish i.e. $glassfish_install_folder\glassfish\lib\ext. Then restart GlassFish.
    • Loggin into the GlassFish admin area using a browser (located at http://localhost:4848 by default).
    • Under Resources > JDBC > Connection Pools, create a new connection pool with a 'Resource Type' of java.sql.Driver.
    • On the 'New JDBC Connection Pool (Step 1 of 2)' screen.
      • Enter a 'Pool Name' (eg SmallBizPool).
      • Select the 'Resource Type' (java.sql.Driver).
      • Select the 'Database Driver Vendor' (MySQL).
      • Next.
    • On the bottom section of the following screen in Additional Properties.
      • URL: jdbc:mysql://localhost:3306/smallbiz
      • user: yourUser (root in my set-up)
      • password: yourPassword
      • Finish
      • Ping on the following screen to test the connection.
    • Create a JDBC Resource from the connection pool at Resources > JDBC > JDBC Resources:
      • Enter a 'JNDI Name' (eg jdbc/SmallBiz). We use this in the persistence.xml file so the application knows how to connect to the database.
      • Select the connection pool previously created from the 'Pool Name' drop down list.
    • For more Information on JDBC Connection Pool and Resource
  • Connect Eclipse to GlassFish.
    • Add GlassFish Tools to Eclipse by searching Help > Eclipse Marketplace.
    • Connect GlassFish to Eclipse by creating a new server using the Servers tab.
      • Select GlassFish 4.0 on the Define a New Server screen.
      • Ensure 'jdk7' i selected and the JDK in the New GlasssFish 4.0 Runtime screen.
      • On the screen that follows, ensure the correct domain directory is selected (the default is domain1, eg. $glassfish_install_folder\glassfish\domains\domain1), then enter the admin credential for GlassFish.
      • You can now interact with GlassFish (start, stop, restart, run on, etc) from in Eclipse.
  • Create a new Maven Project in Eclipse and add dependencies using m2e (which is built in) for:
    • EclipseLink (for JPA) “org.eclipse.persistence eclipselink”

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 TABLE item (
         id VARCHAR(36) NOT NULL,
         itemName TEXT NOT NULL,
         itemDescription TEXT,
         itemPrice DOUBLE,
         PRIMARY KEY (id)
     )
    
  • Create a JPA entity from the database table by right clicking a package created in Eclipse and selecting New > JPA Entities from Table.

  • Annotate the JPA entity with JAXB annotation in order to be able to marshal it to and from xml or json.

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.

package com.zangolie.smallbiz.entities;

import java.io.Serializable;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.annotations.UuidGenerator;


/**
 * The persistent class for the item database table.
 * 
 */
@UuidGenerator(name="UUID")
@XmlRootElement
@Entity
@NamedQuery(name="Item.findAll", query="SELECT i FROM Item i")
public class Item implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator="UUID")
    @Column(length=36)
    private String id;

    private String itemDescription;

    @Lob
    private String itemName;

    private double itemPrice;

    public Item() {
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getItemDescription() {
        return this.itemDescription;
    }

    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }

    public String getItemName() {
        return this.itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public double getItemPrice() {
        return this.itemPrice;
    }

    public void setItemPrice(double itemPrice) {
        this.itemPrice = itemPrice;
    }

}
  • Create folder src\main\webapp\WEB-INF\classes\META-INF and create a persistence.xml file.

    <?xml version="1.0" encoding="UTF-8"?>
    
    <persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
        <persistence-unit name="testPU" transaction-type="JTA">
            <jta-data-source>jdbc/SmallBiz</jta-data-source>
        </persistence-unit>
    </persistence>
    

The RESTful Service Layer

  • Set the Maven Compiler to 1.7
    • Right click the pom.xml file and select Open With > Maven POM Editor. Add the following just before the closing tag.

xml snippet

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • Add dependencies using m2e for:
    • Servlet 3.1 (JAX-RS 2.0 requires this) "javax.servlet javax.servlet.api"
    • Jersey (for JAX-RS) "org.glassfish.jersey.core jersey-sever"
    • EJB (required to use the @Stateless annotation) "javax.ejb javax.ejb.api"
  • Create a POJO (plain old java object) and annotate it with JAX-RS annotations to create an endpoint.
    • @Path("/your-app-api-uri"): To indicate the path, relative to your server base uri, that a resource is located
    • @Produces ({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}): The representation produced can be json or xml.
    • @Consumes ({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}): The representation consumed can be json or xml
    • @Stateless: There server does not store state between interactions (this is one of the principles of the RESTful architecture).
  • Annotate methods that map to CRUD operations
    • @POST: Create
    • @GET: Read
    • @PUT: Update
    • @DELETE: Delete

The JAX-RS Service

package com.zangolie.smallbiz.services.rest;

import java.net.URI;
import java.util.Collection;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import com.zangolie.smallbiz.entities.Item;

@Path("/item")
@Produces ({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Consumes ({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Stateless
public class ItemRestService {
    //the PersistenceContext annotation is a shortcut that hides the fact
    //that, an entity manager is always obtained from an EntityManagerFactory.
    //The peristitence.xml file defines persistence units which is supplied by name
    //to the EntityManagerFactory, thus  dictating settings and classes used by the
    //entity manager
    @PersistenceContext(unitName = "testPU")
    private EntityManager em;

    //Inject UriInfo to build the uri used in the POST response
    @Context
    private UriInfo uriInfo;

    @POST
    public Response createItem(Item item){
        if(item == null){
            throw new BadRequestException();
        }
        em.persist(item);

        //Build a uri with the Item id appended to the absolute path
        //This is so the client gets the Item id and also has the path to the resource created
        URI itemUri = uriInfo.getAbsolutePathBuilder().path(item.getId()).build();

        //The created response will not have a body. The itemUri will be in the Header
        return Response.created(itemUri).build();
    }

    @GET
    @Path("{id}")
    public Response getItem(@PathParam("id") String id){
        Item item = em.find(Item.class, id);

        if(item == null){
            throw new NotFoundException();
        }

        return Response.ok(item).build();
    }

    //Response.ok() does not accept collections
    //But we return a collection and JAX-RS will generate header 200 OK and
    //will handle converting the collection to xml or json as the body
    @GET
    public Collection<Item> getItems(){
        TypedQuery<Item> query = em.createNamedQuery("Item.findAll", Item.class);
        return query.getResultList();
    }

    @PUT
    @Path("{id}")
    public Response updateItem(Item item, @PathParam("id") String id){
        if(id == null){
            throw new BadRequestException();
        }

        //Ideally we should check the id is a valid UUID. Not implementing for now
        item.setId(id);
        em.merge(item);

        return Response.ok().build();
    }

    @DELETE
    @Path("{id}")
    public Response deleteItem(@PathParam("id") String id){
        Item item = em.find(Item.class, id);
        if(item == null){
            throw new NotFoundException();
        }
        em.remove(item);
        return Response.noContent().build();
    }

}
  • Create an Application Class that defines the base uri. eg for http://localhost:8080/smallbiz/rest

    package com.zangolie.smallbiz.services.rest;
    
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    
    @ApplicationPath("rest")
    public class ApplicationConfig extends Application {
    
    }
    
  • Deploying to GlassFish from within Eclipse.

    • Right click the project Run As > Run on Server
    • Select the connected GlassFish server.
  • Test with any HTTP client (such as Postman which works in chrome).

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:

  • Sort out how to connect to it from Eclipse.
  • Change the Maven dependencies for Jersey and EclipseLink from provided to compile (since those are the implementations in GlassFish and may be different in other containers).

Hope it is helpful.

夜深人未静 2024-12-14 07:49:43

这可能正是您正在寻找的:
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.

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