Web编程学习六:使用Apache olingo将JPA数据库操作转化为RESTful Web Service

上次使用了Jersey来创建自己的RESTful web service,但是需要自己来创建每个方法,不是很方便。

今天来使用Apache olingo库来将JPA操作自动转化为RESTful service, Apache olingo是SAP捐给Apache的odata库。

下面来看看olingo和jersey相比,有多么的方便。

注意:在JPA转化RESTful的场景中,还需要使用Apache CXF包。

Olingo的主页:http://olingo.apache.org


开发环境是:Eclipse Juno, MySQL 5.5,olingo 1.2,EclipseLink 2.4, Tomcat 7


1.创建一个Dynamic Web Project,名字叫jpa2

2.加入JPA Facet,在lib中导入EclipseLink、MySql Connector、Olingo、Apache CXF包。

Web编程学习六:使用Apache olingo将JPA数据库操作转化为RESTful Web Service

3.配置JPA的persistence.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
	<persistence-unit name="jpa2" transaction-type="RESOURCE_LOCAL">
		<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
		<class>sample.Employee</class>
		<properties>
			<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:8889/test" />
			<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
			<property name="javax.persistence.jdbc.user" value="root" />
			<property name="javax.persistence.jdbc.password" value="root" />
		</properties>
	</persistence-unit>
</persistence>


4.数据对象Employee.java:

package sample;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Employee {
	@Id
	@GeneratedValue
	private int id;

	@Basic
	private String firstName;

	@Basic
	private String lastName;

	public int getId() {
		return id;
	}

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

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}


5.创建一个继承ODataJPAServiceFactory的类

EmployeeServiceFactory

package sample;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;
import org.apache.olingo.odata2.jpa.processor.api.ODataJPAServiceFactory;
import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;

public class EmployeeServiceFactory extends ODataJPAServiceFactory {
	private static final String PERSISTENCE_UNIT_NAME = "jpa2";

	@Override
	public ODataJPAContext initializeODataJPAContext()
			throws ODataJPARuntimeException {
		ODataJPAContext oDataJPAContext = this.getODataJPAContext();
		try {
			EntityManagerFactory emf = Persistence
					.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
			oDataJPAContext.setEntityManagerFactory(emf);
			oDataJPAContext.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
			return oDataJPAContext;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}


6.创建web.xml,配置一下olingo

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>RestProjectTest</display-name>
	<servlet>
		<servlet-name>ODataServlet</servlet-name>
		<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
		<init-param>
			<param-name>javax.ws.rs.Application</param-name>
			<param-value>org.apache.olingo.odata2.core.rest.app.ODataApplication</param-value>
		</init-param>
		<init-param>
			<param-name>org.apache.olingo.odata2.service.factory</param-name>
			<param-value>sample.EmployeeServiceFactory</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>ODataServlet</servlet-name>
		<url-pattern>/Employee.svc/*</url-pattern>
	</servlet-mapping>

</web-app>  

7.搞定,部署项目到Tomcat,往数据库表employee中输入几条测试数据:

Web编程学习六:使用Apache olingo将JPA数据库操作转化为RESTful Web Service

8.用Chrome的PostMan来测试一下:

8.1查询全部对象

Web编程学习六:使用Apache olingo将JPA数据库操作转化为RESTful Web Service

8.2查询单个对象

Web编程学习六:使用Apache olingo将JPA数据库操作转化为RESTful Web Service

8.3创建一个对象

使用Post方法,向url: http://localhost:8080/jpa2/Employee.svc/Employees发送一个employee entry.

这里我用的RestClient:

Web编程学习六:使用Apache olingo将JPA数据库操作转化为RESTful Web Service

Web编程学习六:使用Apache olingo将JPA数据库操作转化为RESTful Web Service

创建成功后,返回201.

Web编程学习六:使用Apache olingo将JPA数据库操作转化为RESTful Web Service

最后再查询一下,返回结果:

Web编程学习六:使用Apache olingo将JPA数据库操作转化为RESTful Web Service


小结:从这个例子,可以看到olingo非常方便,只需要简单的配置,就可以自动将JPA全部自动转化为RESTful的Web Service了。

Web编程学习六:使用Apache olingo将JPA数据库操作转化为RESTful Web Service,布布扣,bubuko.com

Web编程学习六:使用Apache olingo将JPA数据库操作转化为RESTful Web Service

上一篇:oracle 10g RAC [恢复OCR]


下一篇:SQLite 使用主键,ROWID 及自增列