Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Development/2.4.0/JPA-RS/REST-API"

 
(100 intermediate revisions by 3 users not shown)
Line 1: Line 1:
__NOTOC__
+
[http://www.eclipse.org/eclipselink/documentation/2.4/solutions/restful_jpa003.htm#CHDEGJIG JPA-RS API Documentation version 2.4.0]
= JAX-RS: A RESTful API for JPA=
+
 
+
'''History'''
+
Date
+
Author
+
Description
+
01/10/11
+
dclarke
+
Initial Version
+
 
+
{|{{BMTableStyle}}
+
|-{{BMTHStyle}}
+
! Date
+
! Author
+
! Description
+
|-
+
| Oct 1, 2011 || dclarke || Initial Version
+
|-
+
| Nov 9, 2011 || dclarke || Moved to wiki for community review and feedback.
+
|-
+
| Jan-Mar, 2012 || tware || updates and additions to REST API descriptions
+
|}
+
 
+
==Overview==
+
 
+
This specification will define a RESTful API for dealing with JPA. The intent is to simplify how JPA persistence units can be accessed using REST with JSON or XML formatted messages. A JPA-RS runtime will provide access to all persistence units packaged in the same application that it is running in as well as any dynamic persistence unit that is provisioned within it.
+
 
+
===URI Root : /persistence===
+
The root URI for the RESTful interface is defined in the application's web.xml:
+
  <servlet-mapping>
+
    <servlet-name>Jersey REST Service</servlet-name>
+
    <url-pattern>/persistence/*</url-pattern>
+
  </servlet-mapping>
+
 
+
Here the root URI is 'persistence' to differentiate it from other services
+
 
+
== Persistence Unit Operations==
+
 
+
The JPA-RS URI structure then requires a persistence unit name: '''/persistence/{unit-name}'''. Assuming this is a valid persistence unit in the give JPA-RS application context the following high level operations are available.
+
 
+
* ENTITY: /persistence/{unit-name}/entity
+
* QUERY: /persistence/{unit-name}/query
+
* METAMODEL: /persistence/{unit-name}/metamodel
+
* ADMIN: /persistence/{unit-name}
+
 
+
=== HTTP Method Basics ===
+
 
+
The HTTP methods used in JPA-RS with there basic interpretation in persistence are:
+
 
+
* GET:
+
* PUT: enclosed entity be stored under the supplied Request-URI
+
** INSERT when the PK entity does not exist
+
** UPDATE when the entity does exist
+
* POST: new subordinate of resource identified by the request-URI
+
* DELETE:
+
 
+
The HEAD, TRACE, and CONNECT methods currently have no defined meaning in JPA-RS
+
 
+
===Data Formats: JSON or XML===
+
This REST interface deals with XML and JSON representations of the data equally well. The caller is responsible for using the HTTP header values to indicate the format of the content it is sending (Content-Type = application/json or application/xml) as well as indicating the format of the result it expects (Accept = application/json or application/xml).
+
In cases where no header value is specified JSON will be used by default and in cases where content-type is specified and accept is not the returned format will match the content-type passed in.
+
NOTE: In many REST utilities the accept value is defaulted to application/xml making it the users responsibility to configure this value explicitly.
+
 
+
 
+
===Web Caching===
+
In addition to the internal caching within TopLink (EclipseLink) the results from REST URI calls can be cached in various points between the initiating server and the user, including their browser. This caching is determined by URI structure and the HTTP header information in the response from the REST calls. Generally only GET call responses are cached so these must be addressed carefully to ensure the proper caching information is provided so that the end user of the RESTful persistence interface get the most correct information possible while still benefiting from web caching.
+
TODO: Add guidelines
+
 
+
===HTTP Header Fields ===
+
 
+
'''Standard HTTP'''
+
* If-Match - conditional
+
* Warning
+
 
+
'''EclipseLink JPA-RS'''
+
* Refresh
+
 
+
== Entity Operations: /persistence/{unit-name}/entity/* ==
+
 
+
Entity operations are those performed against a specific entity type within the persistence unit. The {type} value refers to the type name (descriptor alias).
+
 
+
=== FIND ===
+
 
+
* GET /persistence/{unit-name}/entity/{type}{id}
+
** Refresh = GET + Header(Refresh)
+
** {id} is currently defined using HTTP query parameters
+
** e.g. http://localhost:8080/persistence/ExamplePU/entity/Foo?id=1
+
* Produces: JSON or XML
+
* Response
+
** OK, Payload: Entity
+
** NOT_FOUND if Entity does not exist
+
 
+
// TODO id should not be specified with query parameters. We should use Matrix parameters or the URL itself
+
 
+
'''EntityManager.find API:'''
+
<pre>
+
public <T> T find(Class<T> entityClass, Object primaryKey);
+
public <T> T find(Class<T> entityClass, Object primaryKey, Map<String, Object> properties);
+
</pre>
+
 
+
The JPA-RS GET with format
+
 
+
==== Composite Key ====
+
 
+
// TODO
+
 
+
==== Result Caching ====
+
 
+
// TODO
+
 
+
==== REFRESH ====
+
 
+
The EntityManager.refresh operation can be invoked using the find (GET) with the addition of the '''Refresh''' header field.
+
 
+
// TODO
+
 
+
=== PERSIST ===
+
 
+
* PUT /persistence/{unit-name}/entity/{type}
+
** e.g. PUT http://localhost:8080/persistence/ExamplePU/entity/Foo
+
* Consumes: JSON or XML
+
* Payload: Entity
+
* Produces: JSON or XML
+
* Response:
+
** Payload: Entity returned by Persist
+
 
+
=== MERGE ===
+
 
+
* POST /persistence/{unit-name}/entity/{type}
+
** e.g. POST http://localhost:8080/persistence/ExamplePU/entity/Foo
+
* Consumes: JSON or XML
+
* Payload: Entity
+
* Produces: JSON or XML
+
* Response:
+
** Payload: Entity returned by Merge
+
 
+
// TODO: POST should be itempotent, but merge can result in an insert with sequencing.  We need to deal with this issue
+
 
+
=== DELETE ===
+
 
+
* DELETE /persistence/{unit-name}/entity/{type}{id}
+
** {id} is currently defined using HTTP query parameters
+
** e.g. http://localhost:8080/persistence/ExamplePU/entity/Foo?id=1
+
* Response
+
** OK
+
 
+
== Query Operations ==
+
 
+
* Named Query: GET /persistence/{unit-name}/query/{name}{params}
+
* Dynamic Query: GET /persistence/{unit-name}/query
+
** Dynamic JPQL query in payload
+
** Dynamic native query in payload
+
 
+
=== Named Query ===
+
 
+
Named queries doing reads can be run two ways in JPA.  Both are supported in the REST API.
+
 
+
==== List of Results ====
+
 
+
* GET /persistence/{unit-name}/query/{name}<parameters><hints>
+
** <parameters> are specified using HTTP matrix parameters
+
** <hints> are specified using HTTP query parameters and with the key being the name of the EclipseLink query hint
+
** e.g. http://localhost:8080/persistence/ExamplePU/query/Foo.findByName;name=myname
+
** e.g. http://localhost:8080/persistence/ExamplePU/query/Foo.findByName;name=myname?eclipselink.jdbc.max-results=500
+
* Produces: JSON or XML
+
* Response
+
** Payload: List of Entities
+
 
+
==== Single Result ====
+
 
+
* GET /persistence/{unit-name}/querySingleResult/{name}<parameters><hints>
+
** <parameters> are specified using HTTP matrix parameters
+
** <hints> are specified using HTTP query parameters and with the key being the name of the EclipseLink query hint
+
** e.g. http://localhost:8080/persistence/ExamplePU/querySingleResult/Foo.findByName;name=myname
+
* Produces: JSON or XML
+
* Response
+
** Payload: Entity
+
 
+
==== Update/Delete Query ====
+
 
+
* POST /persistence/{unit-name}/query/{name}<parameters><hints>
+
** <parameters> are specified using HTTP matrix parameters
+
** <hints> are specified using HTTP query parameters and with the key being the name of the EclipseLink query hint
+
** e.g. http://localhost:8080/persistence/ExamplePU/query/Foo.deleteAllByName;name=myname
+
** e.g. http://localhost:8080/persistence/ExamplePU/query/Foo.updateName;name=myname?eclipselink.jdbc.max-results=500
+
* Produces: application/octet-stream
+
* Response
+
** Payload:  Number of entities updated or deleted in
+
 
+
=== Dynamic Query ===
+
 
+
// TODO
+
 
+
== Metamodel Operations ==
+
 
+
* GET /persistence/{unit-name}/metamodel
+
* GET /persistence/{unit-name}/metamodel/entity/{type}
+
* GET /persistence/{unit-name}/metamodel/query/{name}
+
 
+
== Admin Operations ==
+
 
+
The admin operations, which can be disabled in a given deployment, focus on persistence unit level administration operations:
+
 
+
* Create
+
** Dynamic Persistence Unit: PUT: /persistence/{unit-name}
+
** Add Dynamic Type: POST: /persistence/{unit-name}/
+
* Delete Persistence Unit: DELETE /persistence/{unit-name}
+
 
+
=== Create Dynamic Persistence Unit ===
+
 
+
Creating a dynamic persistence unit within a JPA-RS runtime involves passing in the necessary EclipseLink metadata and having the PU realized using dynamic entities.
+
 
+
* PUT /persistence/{unit-name}
+
** e.g. PUT http://localhost:8080/persistence/ExamplePU
+
* Payload: Key/Value or XML - pick one
+
** persistenceXmlURL = <URL>
+
** full text of persistence.xml
+
* Response:
+
** CREATED - on success
+
** NOT_FOUND - on failure
+
 
+
=== Bootstrap from stored Persistence Units ===
+
 
+
This option will start the REST service, look in a datastore for any stored persistence units and make those available.  Any persistence units that are bootstrapped will be added to the datastore
+
 
+
* PUT /persistence
+
** e.g. PUT http://localhost:8080/persistence
+
* Payload: Key/Value
+
** datasourceName = DatasourceJNDIName
+
* Response:
+
** CREATED - on success
+
** NOT_FOUND - on failure
+
 
+
=== List Existing Persistence Units ===
+
 
+
* GET /persistence
+
** e.g. http://localhost:8080/persistence/
+
* Produces: JSON
+
* Response
+
** Payload: List of Persistence Unit Names
+
 
+
=== List Types in a Persistence Unit ===
+
 
+
* GET /persistence/{unit-name}
+
** e.g. http://localhost:8080/persistence/ExamplePU
+
* Produces: JSON
+
* Response
+
** OK, Payload: List of Types
+
** NOT_FOUND if persistence unit is not found
+
 
+
=== Delete Persistence Unit ===
+
 
+
* DELETE /persistence/{unit-name}
+
** e.g. DELETE http://localhost:8080/persistence/ExamplePU
+
* Produces: JSON
+
* Response
+
** OK
+
 
+
==Using JPA-RS==
+
 
+
JPA-RS comes packaged in a single Jar.  The key piece of functionality is a web service found in a class called: org.eclipse.persistence.jpa.rs.Service.  Although there is no reason the web service cannot be enabled on any fairly recent container, initial development and testing was done on GlassFish 3.1.2 and this description will assume you are using GlassFish 3.1.2 or better.
+
 
+
===Packaging==
+
 
+
In order to deploy the JPA RS, it must be somehow deployed as part of your web application.  The simplest way to do that is to package it in a war file.  The key things you have to include are as follows:
+
 
+
A web.xml file pointing at the web service.  Here is an example:
+
 
+
<pre>
+
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
+
  <display-name>Henley Avatar Demo</display-name>
+
  <welcome-file-list>
+
    <welcome-file>index.html</welcome-file>
+
  </welcome-file-list>
+
  <servlet>
+
    <servlet-name>JPA-RS Service</servlet-name>
+
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
+
    <init-param>
+
      <param-name>com.sun.jersey.config.property.packages</param-name>
+
      <param-value>org.eclipse.persistence.jpa.rs</param-value>
+
    </init-param>
+
    <load-on-startup>1</load-on-startup>
+
  </servlet>
+
  <servlet-mapping>
+
    <servlet-name>JPA-RS Service</servlet-name>
+
    <url-pattern>/jpa-rs/*</url-pattern>
+
  </servlet-mapping>
+
</web-app>
+
</pre>
+

Latest revision as of 10:22, 9 April 2013

JPA-RS API Documentation version 2.4.0

Back to the top