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"

(Update/Delete Query)
Line 19: Line 19:
 
|-
 
|-
 
| Nov 9, 2011 || dclarke || Moved to wiki for community review and feedback.
 
| Nov 9, 2011 || dclarke || Moved to wiki for community review and feedback.
 +
|-
 +
| Jan-Mar, 2012 || tware || updates and additions to REST API descriptions
 
|}
 
|}
  
Line 255: Line 257:
 
* Response
 
* Response
 
** OK
 
** 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:
 +
 +
 +
<code>
 +
<?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>
 +
</code>

Revision as of 10:37, 12 March 2012

JAX-RS: A RESTful API for JPA

History Date Author Description 01/10/11 dclarke Initial Version

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

// TODO id should not be specified with query parameters. We should use Matrix parameters or the URL itself

EntityManager.find API:

public <T> T find(Class<T> entityClass, Object primaryKey);
public <T> T find(Class<T> entityClass, Object primaryKey, Map<String, Object> properties);

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

MERGE

// TODO: POST should be itempotent, but merge can result in an insert with sequencing. We need to deal with this issue

DELETE

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

Single Result

Update/Delete Query

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}
  • 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
  • Payload: Key/Value
    • datasourceName = DatasourceJNDIName
  • Response:
    • CREATED - on success
    • NOT_FOUND - on failure

List Existing Persistence Units

List Types in a Persistence Unit

Delete Persistence Unit

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:


<?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>

Back to the top