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包。


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中输入几条测试数据:


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

8.1查询全部对象


8.2查询单个对象


8.3创建一个对象

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

这里我用的RestClient:

创建成功后,返回201.

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


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

Web编程学习六:使用Apache olingo将JPA数据库操作转化为RESTful Web Service,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。