
Try out JSON-B 1.0, JPA 2.2 using Java EE 8 on Glassfish 5 & Docker
Posted by Abhishek Gupta-Oracle in Developer Solutions on Jul 13, 2017 11:31:03 AMGlassfish 5 Build 11 is now available - with support for many of the Java EE 8 specifications e.g. JAX-RS 2.1, JPA 2.2, JSON-B 1.0 etc. For more details check out the Aquarium space. This blog covers
- test driving couple of new additions in this build - JSON-B 1.0 and JPA 2.2
- we run this on Oracle Container Cloud, thanks to the Glassfish 5 nightly Docker image
Application
It's a simple one
- Has a REST endpoint (also a @Stateless bean)
- Interacts with an embedded (Derby) DB using JPA - we use the jdbc/__TimerPool present in Glassfish to make things easier
- Test data is bootstrapped using standard JPA features in persistence.xml (drop + create DB along with a SQL source)
JSON-B 1.0 in action
Primarily makes use of the JSON-B annotations to customize the behavior
- @JsonbProperty to modify the name of the JSON attribute i.e. its different as compared to the POJO field/variable name
- @JsonbPropertyOrder to specify the lexicographical reverse (Z to A) order for JSON attributes
For more, check out Yasson which is the reference implementation
JPA 2.2 in action
The sample application uses the stream result feature added to Query and TypedQuery interfaces by which it's possible to use JDK 8 Streams API to navigate the result set of a JPA (JPQL, native etc.) query. For other additions in JPA 2.2, please check this
Build the Docker image
- git clone https://github.com/abhirockzz/javaee8-jsonb-jpa.git
- mvn clean install - this will produce javaee8-jsonb-jpa.war in target directory
- docker build -t <image_name> . - this will create a Docker image
- docker push <image_name>
Shortcut
Use an existing image from Docker Hub - docker pull abhirockzz/javaee-jsonb-jpa
Run on Oracle Container Cloud
You can use this section of one of my previous blog or the documentation (create a service, deploy) to get this up and running on Oracle Container Cloud. It's super simple
Create a service where you reference the Docker image
Post service creation
Initiate a deployment.. and that's it ! You'll see something similar to this
Test things out
Please make a note of the Host IP of your Oracle Container Cloud worker node (basically a compute VM)
Fetch all employees
http://<OCCS_HOST_IP>:8080/javaee8-jsonb-jpa/
You will get a JSON payload with all employees
[ { "salary": 100, "name": "abhi", "emp_email": "abhirockzz@gmail.com" }, { "salary": 99, "name": "rockzz", "emp_email": "kehsihba@hotmail.com" } ]
Fetch an employee
http://<OCCS_HOST_IP>:8080/javaee8-jsonb-jpa/abhirockzz@gmail.com
You will see a JSON payload in as a response
{ "salary": 100, "name": "abhi", "emp_email": "abhirockzz@gmail.com" }
Enjoy Java EE 8 and Glassfish !
**The views expressed in this post are my own and do not necessarily reflect the views of Oracle
Comments