如何使用 JAX 在 Java 中使用 Web 服务从数据库中插入数据 - RS
我是网络服务的新手.请给出建议如何在 java 中使用 jersey JAX - RS 从数据库中插入和检索数据?
I am new to web services. Please give suggestions how to insert and retrieve data from database using jersey JAX - RS in java?
推荐答案
下面是一个 JAX-RS 服务的示例,它使用 JPA 实现持久化作为会话 bean和 JAXB 用于消息传递可能看起来像.
Below is an example of a JAX-RS service implemented as a session bean using JPA for persistence and JAXB for messaging might look like.
客户服务
package org.example;
import java.util.List;
import javax.ejb.*;
import javax.persistence.*;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Stateless
@LocalBean
@Path("/customers")
public class CustomerService {
@PersistenceContext(unitName="CustomerService",
type=PersistenceContextType.TRANSACTION)
EntityManager entityManager;
@POST
@Consumes(MediaType.APPLICATION_XML)
public void create(Customer customer) {
entityManager.persist(customer);
}
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("{id}")
public Customer read(@PathParam("id") long id) {
return entityManager.find(Customer.class, id);
}
@PUT
@Consumes(MediaType.APPLICATION_XML)
public void update(Customer customer) {
entityManager.merge(customer);
}
@DELETE
@Path("{id}")
public void delete(@PathParam("id") long id) {
Customer customer = read(id);
if(null != customer) {
entityManager.remove(customer);
}
}
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("findCustomersByCity/{city}")
public List<Customer> findCustomersByCity(@PathParam("city") String city) {
Query query = entityManager.createNamedQuery("findCustomersByCity");
query.setParameter("city", city);
return query.getResultList();
}
}
客户
以下是其中一个实体的示例.它包含 JPA 和 JAXB 注释.
Below is an example of one of the entities. It contains both JPA and JAXB annotations.
package org.example;
import java.io.Serializable;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Set;
@Entity
@NamedQuery(name = "findCustomersByCity",
query = "SELECT c " +
"FROM Customer c " +
"WHERE c.address.city = :city")
@XmlRootElement
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private long id;
@Column(name="FIRST_NAME")
private String firstName;
@Column(name="LAST_NAME")
private String lastName;
@OneToOne(mappedBy="customer", cascade={CascadeType.ALL})
private Address address;
@OneToMany(mappedBy="customer", cascade={CascadeType.ALL})
private Set<PhoneNumber> phoneNumbers;
}
更多信息
- 第 1 部分 - 数据模型
- 第 2 部分 - JPA
- 第 3 部分 - JAXB(使用 MOXy)
- 第 4 部分 - RESTful 服务(使用EJB 会话 bean)
- 第 5 部分 - 客户端
更新
需要什么罐子
您可以将 JAX-RS/EJB/JPA/JAXB 应用程序部署到任何符合 Java EE 6 的应用程序服务器,而无需设置任何额外的服务器.对客户端进行编程,您可以从 Jersey 获取 JAX-RS API (http://jersey.java.net/),以及来自 EclipseLink 的 JPA 和 JAXB API (http://www.eclipse.org/eclipselink/).
You can deploy a JAX-RS/EJB/JPA/JAXB application to any Java EE 6 compliant application server without requiring any additional server set up. Programming the client you can get the JAX-RS APIs from the Jersey (http://jersey.java.net/), and the JPA and JAXB APIs from EclipseLink (http://www.eclipse.org/eclipselink/).
以及数据库连接的写入位置
and where the database connections is written
JDBC 资源 &连接池
您需要在应用服务器上配置一个连接池.以下是在 GlassFish 上执行此操作的步骤.这些步骤会因您使用的应用程序服务器而异.
You need to configure a connection pool on your application server. Below are the steps to do this on GlassFish. The steps will vary depending on the application server you are using.
- 将 JDBC 驱动程序 (ojdbc14.jar) 复制到/glashfish/lib
- 启动管理控制台
- 创建连接池:
- 姓名 = 客户服务
- 资源类型 = 'javax.sql.ConnectionPoolDataSource'
- 数据库供应商 = Oracle(或任何适合您的数据库的数据库供应商)
- 点击下一步,填写以下附加属性":
- 用户(例如 CustomerService)
- 密码(例如密码)
- 网址(例如 jdbc:oracle:thin:@localhost:1521:XE)
- 使用Ping"按钮测试您的数据库连接
- JNDI 名称 = CustomerService
- Pool Name = CustomerService(您在上一步中创建的连接池的名称)
JPA 配置
然后我们在 persistence.xml
文件中为我们的 JPA 实体引用我们上面创建的数据库连接,如下所示:
Then we reference the database connection we created above in the persistence.xml
file for our JPA entities as follows:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="CustomerService" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>CustomerService</jta-data-source>
<class>org.example.Customer</class>
<class>org.example.Address</class>
<class>org.example.PhoneNumber</class>
<properties>
<property name="eclipselink.target-database" value="Oracle" />
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.logging.level.ejb_or_metadata" value="WARNING" />
<property name="eclipselink.logging.timestamp" value="false"/>
<property name="eclipselink.logging.thread" value="false"/>
<property name="eclipselink.logging.session" value="false"/>
<property name="eclipselink.logging.exceptions" value="false"/>
<property name="eclipselink.target-server" value="SunAS9"/>
</properties>
</persistence-unit>
</persistence>
相关文章