SlideShare une entreprise Scribd logo
1  sur  28
Télécharger pour lire hors ligne
REST API DESIGN AND CONSTRUCTION WITH
JAVA EE
PAGES FROM MY WORK DIARY

By Vineet Reynolds L P / @VineetReynolds
Senior Software Engineer
JBoss Developer Experience, Red Hat
ABOUT THE SPEAKER
Engineer in the JBoss Developer Experience team
Currently, contributor to :
JBoss Forge 1.x and 2.0
AngularJS scaffolding for JBoss Forge
JDF quickstarts
TicketMonster
Others:
Arquillian (GlassFish, WebLogic container adapters)
AGENDA
A 10 minute guide to REST
JPA and JAX­RS
Writing a data­driven REST API with JPA and JAX­RS
Composite keys
Cyclic and bi­directional relationships
Lazy­fetching
Class hierarchies (java.sql.Date)
Bean Validation and error reporting
A 10 MINUTE GUIDE TO REST
Note ­ this is really a 10 minute guide. This is not a deep­dive
into Roy Fielding's doctoral dissertation.
WHAT IS REST ?
REST = REpresentational State Transfer
It is an architectural style.
It describes the design of a hypermedia system (the web).
It intends to conceptualize how a well designed Web
application should behave.
EXAMPLES
GT/vns
E   e e t  returns all Events.
GT/vns1
E   e e t /  returns the Event with ID 1.
P S   e e t  creates a new Event.
OT/vns
PT/vns1
U   e e t /  updates the Event.
D L T   e e t /  deletes the Event.
EEE/vns1
EXAMPLES
Assuming G T / v n s 1
E   e e t /  returns
                
                

{
  "ae  JDO"
  nm":"UCN,
  "esos  
  ssin":[
          {"ae  Arga"}
           nm":"eoer ,
          {"ae  Ifnsa"}
           nm":"niipn ,
          {"ae  OeSit ,
           nm":"pnhf"}
          {"ae  WlFy 
           nm":"idl"}
         
        ]
}
              
              

What kind of updates can be made in a PUT ?
Session names could be updated.
New Sessions could be added.
Existing sessions could be deleted.
JAX-RS + JPA + EJB : THE BASICS
Let's expose a JPA entity as a REST resource.
              
              

@niy
Ett
pbi ls vn 
ulccasEet{
  @d
  I
  @eeaeVlesrtg  eeainyeAT)
  Gnrtdau(taey=GnrtoTp.UO
  piaeLn d=nl;
  rvt ogi  ul
  @eso
  Vrin
  piaeitvrin=0
  rvt n eso  ;
  @oun
  Clm
  piaeSrn ae
  rvt tignm;
  / etr,Stes qas)adhsCd(
  /Gtes etr,eul( n ahoe)
  ..
  .
}            
            
 
JAX-RS + JPA + EJB : THE BASICS
CREATING NEW EVENTS
              
              

PS sml­vn/eteet TP11
OT/ipeeetrs/vnsHT/.
Hs:lclot88
ot oahs:00
CnetTp:apiainjo;hre=T­
otn­ye plcto/sncastUF8
..
.
{nm""UCN}
"ae:JDO"
            
            
              
              

HT/. 0 rae
TP1121Cetd
Lcto:ht:/oahs:00sml­vn/eteet/
oain tp/lclot88/ipeeetrs/vns1
..
.
            
            
JAX-RS + JPA + EJB : THE BASICS
FETCHING ALL EVENTS
              
              

GT/ipeeetrs/vnsHT/.
E sml­vn/eteet TP11
Hs:lclot88
ot oahs:00
Acp:apiainjo,tx/li,**
cet plcto/sn etpan /
..
.
            
            
              
              

HT/. 0 K
TP1120O
CnetTp:apiainjo
otn­ye plcto/sn
..
.
["d:,vrin:,nm""UCN}
{i"1"eso"0"ae:JDO"]
            
            
JAX-RS + JPA + EJB : THE BASICS
FETCHING ONE SPECIFIC EVENT
              
              

GT/ipeeetrs/vns1HT/.
E sml­vn/eteet/ TP11
Hs:lclot88
ot oahs:00
Acp:apiainjo,tx/li,**
cet plcto/sn etpan /
..
.
            
            
              
              

HT/. 0 K
TP1120O
CnetTp:apiainjo
otn­ye plcto/sn
..
.
{i"1"eso"0"ae:JDO"
"d:,vrin:,nm""UCN}
            
            
JAX-RS + JPA + EJB : THE BASICS
UPDATING AN EVENT
              
              

PT/ipeeetrs/vns1HT/.
U sml­vn/eteet/ TP11
Hs:lclot88
ot oahs:00
Acp:apiainjo,tx/li,**
cet plcto/sn etpan /
..
.
{i"1"eso"0"ae:JDO ni"
"d:,vrin:,nm""UCNIda}
            
            
              
              

HT/. 0 oCnet
TP1124N otn
..
.
            
            
JAX-RS + JPA + EJB : THE BASICS
DELETING AN EVENT
              
              

DLT sml­vn/eteet/ TP11
EEE/ipeeetrs/vns1HT/.
Hs:lclot88
ot oahs:00
Acp:apiainjo,tx/li,**
cet plcto/sn etpan /
..
.
            
            
              
              

HT/. 0 oCnet
TP1124N otn
..
.
            
            
COMPOSITE KEYS ARE NOT FUN
Addressability suffers. Matrix parameters will help with
composite keys.
            
            

/ e o vn  ae oain
/Kyfreet=nm,lcto
GT/vnsnm=UCNlcto=ni
E eet;aeJDO;oainIda
          
          

But ...
They're not a standard. Just a W3C design note.
Lack of client support (but improving). Among popular JS
libraries, only Dojo, AngularJS, Backbone.js has support.
If cheap, establish a mapping scheme for composite keys.
RELATIONSHIPS
Handling modifications to relationships in JPA entities can get
difficult.
CREATE A NEW EVENT
              
              

PS bd­vn/eteet TP11
OT/iieetrs/vnsHT/.
Hs:lclot88
ot oahs:00
Acp:apiainjo,tx/li,**
cet plcto/sn etpan /
..
.
{nm""UCN,lcto""aglr"
"ae:JDO""oain:Bnaoe}
            
            
              
              

HT/. 0 rae
TP1121Cetd
Lcto:ht:/oahs:00bd­vn/eteet/
oain tp/lclot88/iieetrs/vns1
..
.
            
            
FETCH AN EXISTING EVENT
              
              

GT/iieetrs/vns1HT/.
E bd­vn/eteet/ TP11
Hs:lclot88
ot oahs:00
Acp:apiainjo,tx/li,**
cet plcto/sn etpan /
..
.
            
            
              
              

HT/. 0 K
TP1120O
CnetTp:apiainjo
otn­ye plcto/sn
..
.
{i"1"eso"0"ae:JDO""oain:Bnaoe,ssin"[}
"d:,vrin:,nm""UCN,lcto""aglr""esos:]
            
            
CREATE A NEW SESSION
              
              

PS bd­vn/etssin TP11
OT/iieetrs/esosHT/.
Hs:lclot88
ot oahs:00
Acp:apiainjo,tx/li,**
cet plcto/sn etpan /
{
  "esoNm""eoer,
  ssinae:Arga"
  "pae""eata"
  sekr:Sbsin,
  "vn"{i"1
  eet:"d:,
      "eso"0
      vrin:,
      "ae:JDO"
      nm""UCN,
      "oain:Bnaoe,
      lcto""aglr"
      "esos:]
      ssin"[}
}
            
            
              
              

HT/. 0 rae
TP1121Cetd
Lcto:ht:/oahs:00bd­vn/etssin/
oain tp/lclot88/iieetrs/esos2
..
.
            
            
FETCH THE NEWLY CREATED SESSION
              
              

GT/iieetrs/esos2HT/.
E bd­vn/etssin/ TP11
Hs:lclot88
ot oahs:00
Acp:apiainjo,tx/li,**
cet plcto/sn etpan /
..
.
            
            
              
              

HT/. 0 nenlSre ro
TP1150Itra evrErr
CnetTp:tx/tlcastuf8
otn­ye ethm;hre=t­

JWB005 TPSau 0  r.oeasjcsnmpJoMpigxeto
BE006:HT tts50­ogcdhu.ako.a.snapnEcpi
fie olzl ntaieacleto frl:cmeapebdeetmd
aldt aiyiiilz  olcino oe o.xml.iivn.o
n eso truhrfrnecan o.xml.iivn.oe.eso[e
oSsin(hog eeec hi:cmeapebdeetmdlSsin"
            
            
WHAT JUST HAPPENED?
E e t s s i n  is lazily fetched.
vn.esos
During serialization, the proxy does not find an active
session.
Let's try and solve this crudely, using eager fetching ...
              
              

{i"2"eso"0"esoNm""eoer,sekr:Sbsin,eet:
"d:,vrin:,ssinae:Arga""pae""eata""vn"
["d:,vrin:,ssinae:Arga""pae""eata""vn"
{i"2"eso"0"esoNm""eoer,sekr:Sbsin,eet
["d:,vrin:,ssinae:Arga""pae""eata""vn"
{i"2"eso"0"esoNm""eoer,sekr:Sbsin,eet
..
.
/ hsnvrtriae
/Ti ee emnts
            
            
HOW DO YOU SOLVE THIS ?
If you can, do not expose collection members of JPA entities
directly.
Use sub­resources for dependent collections (1:M).
Use a mapping layer. Create Representation classes to map
JPA object graphs.
The mappings translate RESTful actions into operations in
the domain model.
Or, use different representations of the same JPA entity for
different operations (not a good idea).
One to create an entity with collections, another to update
only the entity.
Model collections as a separate resource.
Makes sense for M:N relationships.
JAVA.SQL.* HATES YOU
Consider the following
                
                

@eprlTmoaTp.IE
Tmoa(eprlyeTM)
jv.tlDt trTm;
aaui.aesatie
              
              

During serialization, s a t i e
t r T m 's type is j v . q . i e
aaslTm.
Given a value of 2:00 PM IST, the field will be serialized as
{satie:1:00".
"trTm""40:0}
When deserializing back, { s a t i e : 1 7 ­ 1
"trTm""900­
0T40:0000:0}
1 1 : 0 0 . 0 + 5 3 "  should be supplied.
JAVA.SQL.* HATES YOU
Before, we discuss solutions, let's talk about:
ISO8601 vs Unix timestamps. Which one to use ?
The former is almost always better.
Date, time and timestamp formats. Omit unnecessary info in
ISO8601 formats (like time values for Dates).
JAVA.SQL.* HATES YOU
Lessons learnt ­
Be aware of the actual types of the objects during
serialization and deserialization.
Constrain the actual types and hence the
serializers/deserializers used.
When writing custom serializers, design them for class
hierarchies.
Think of the equals() contract, but for serialized forms.
And of course, avoid java.util.Date/Calendar. Use specific
types in Joda­Time and eventually JSR­310 (Java 8).
Using a specific type binds a serializer + deserializer to the
field.
BEAN VALIDATION AND VIOLATION REPORTING
Responses for Constraint violation errors on JPA entities are
not standardized.
By default, HTML responses will be provided if no
E c p i n a p r  exist for
xetoMpes
CntanVoainxetos
osritiltoEcpin.
Bean Validation for request parameters is available out­of­
the­box, but requires activation.
RESTEasy has one format, Jersey has another.
QUESTIONS ?
REST API design and construction with Java EE - pages from my work diary

Contenu connexe

Tendances

Dileep_Varanasi_Oracle Apps_Consultant
Dileep_Varanasi_Oracle Apps_ConsultantDileep_Varanasi_Oracle Apps_Consultant
Dileep_Varanasi_Oracle Apps_Consultant
Dileep kumar varanasi
 
Jagan_Updated Resume
Jagan_Updated ResumeJagan_Updated Resume
Jagan_Updated Resume
Jagan Mohan
 
Karan_Verma_Resume
Karan_Verma_ResumeKaran_Verma_Resume
Karan_Verma_Resume
Karan Verma
 
Stan_Wang_Resume
Stan_Wang_ResumeStan_Wang_Resume
Stan_Wang_Resume
Jia Wang
 

Tendances (20)

AbhishekMalik_CV_12Dec2018
AbhishekMalik_CV_12Dec2018AbhishekMalik_CV_12Dec2018
AbhishekMalik_CV_12Dec2018
 
Resume joseph gregory java
Resume   joseph gregory javaResume   joseph gregory java
Resume joseph gregory java
 
AbhishekMalik_CV_12Dec2018
AbhishekMalik_CV_12Dec2018AbhishekMalik_CV_12Dec2018
AbhishekMalik_CV_12Dec2018
 
Dileep_Varanasi_Oracle Apps_Consultant
Dileep_Varanasi_Oracle Apps_ConsultantDileep_Varanasi_Oracle Apps_Consultant
Dileep_Varanasi_Oracle Apps_Consultant
 
Comparison Of Open Source App Servers
Comparison Of Open Source App ServersComparison Of Open Source App Servers
Comparison Of Open Source App Servers
 
VeenaChilukala
VeenaChilukalaVeenaChilukala
VeenaChilukala
 
Sunil kumar resume
Sunil kumar resumeSunil kumar resume
Sunil kumar resume
 
Jagan_Updated Resume
Jagan_Updated ResumeJagan_Updated Resume
Jagan_Updated Resume
 
Karan_Verma_Resume
Karan_Verma_ResumeKaran_Verma_Resume
Karan_Verma_Resume
 
Ruby vs Java: Comparing Two Popular Programming Languages For Developing Ente...
Ruby vs Java: Comparing Two Popular Programming Languages For Developing Ente...Ruby vs Java: Comparing Two Popular Programming Languages For Developing Ente...
Ruby vs Java: Comparing Two Popular Programming Languages For Developing Ente...
 
Rajesh Ramasamy
Rajesh RamasamyRajesh Ramasamy
Rajesh Ramasamy
 
77402_VishalLaljeet
77402_VishalLaljeet77402_VishalLaljeet
77402_VishalLaljeet
 
KumarJava
KumarJavaKumarJava
KumarJava
 
SurajResume
SurajResumeSurajResume
SurajResume
 
Real World Technologies
Real World TechnologiesReal World Technologies
Real World Technologies
 
How To Find Resumes On Google | Boolean Search | Image Search Java Developer...
How To Find Resumes On Google  | Boolean Search | Image Search Java Developer...How To Find Resumes On Google  | Boolean Search | Image Search Java Developer...
How To Find Resumes On Google | Boolean Search | Image Search Java Developer...
 
Technical Recruitment Overview & Tips
Technical Recruitment Overview & TipsTechnical Recruitment Overview & Tips
Technical Recruitment Overview & Tips
 
Rajiv ranjan resume-us
Rajiv ranjan  resume-usRajiv ranjan  resume-us
Rajiv ranjan resume-us
 
AliExpress’ Way to Microservices - microXchg 2017
AliExpress’ Way to Microservices  - microXchg 2017AliExpress’ Way to Microservices  - microXchg 2017
AliExpress’ Way to Microservices - microXchg 2017
 
Stan_Wang_Resume
Stan_Wang_ResumeStan_Wang_Resume
Stan_Wang_Resume
 

En vedette

Web Technologies in Java EE 7
Web Technologies in Java EE 7Web Technologies in Java EE 7
Web Technologies in Java EE 7
Lukáš Fryč
 

En vedette (14)

Gwt.create
Gwt.createGwt.create
Gwt.create
 
From JavaEE to AngularJS
From JavaEE to AngularJSFrom JavaEE to AngularJS
From JavaEE to AngularJS
 
GUJavaSC - Combinando AngularJS com Java EE
GUJavaSC - Combinando AngularJS com Java EEGUJavaSC - Combinando AngularJS com Java EE
GUJavaSC - Combinando AngularJS com Java EE
 
Web Technologies in Java EE 7
Web Technologies in Java EE 7Web Technologies in Java EE 7
Web Technologies in Java EE 7
 
RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RS
 
Tech Meetup: How to build a Rest API in Java
Tech Meetup: How to build a Rest API in JavaTech Meetup: How to build a Rest API in Java
Tech Meetup: How to build a Rest API in Java
 
QCon 2015 - Combinando AngularJS com Java EE
QCon 2015 - Combinando AngularJS com Java EEQCon 2015 - Combinando AngularJS com Java EE
QCon 2015 - Combinando AngularJS com Java EE
 
AngularJS for Java Developers
AngularJS for Java DevelopersAngularJS for Java Developers
AngularJS for Java Developers
 
Making Java REST with JAX-RS 2.0
Making Java REST with JAX-RS 2.0Making Java REST with JAX-RS 2.0
Making Java REST with JAX-RS 2.0
 
Developing Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJSDeveloping Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJS
 
AngularJS application architecture
AngularJS application architectureAngularJS application architecture
AngularJS application architecture
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS Architecture
 
JavaScript Frameworks and Java EE – A Great Match
JavaScript Frameworks and Java EE – A Great MatchJavaScript Frameworks and Java EE – A Great Match
JavaScript Frameworks and Java EE – A Great Match
 
Modern web application development with java ee 7
Modern web application development with java ee 7Modern web application development with java ee 7
Modern web application development with java ee 7
 

Similaire à REST API design and construction with Java EE - pages from my work diary

Wakanda - apps.berlin.js - 2012-11-29
Wakanda - apps.berlin.js - 2012-11-29Wakanda - apps.berlin.js - 2012-11-29
Wakanda - apps.berlin.js - 2012-11-29
Alexandre Morgaut
 

Similaire à REST API design and construction with Java EE - pages from my work diary (20)

Jspx Jdc2010
Jspx Jdc2010Jspx Jdc2010
Jspx Jdc2010
 
Reactで話題のRecoilを使ってみた
Reactで話題のRecoilを使ってみたReactで話題のRecoilを使ってみた
Reactで話題のRecoilを使ってみた
 
Web Applications of the future: Combining JEE6 & JavaFX
Web Applications of the future: Combining JEE6 & JavaFXWeb Applications of the future: Combining JEE6 & JavaFX
Web Applications of the future: Combining JEE6 & JavaFX
 
Vishnu(java)
Vishnu(java)Vishnu(java)
Vishnu(java)
 
Review on React JS
Review on React JSReview on React JS
Review on React JS
 
Netbeans 6.1 Talk
Netbeans 6.1 TalkNetbeans 6.1 Talk
Netbeans 6.1 Talk
 
Difference between React JS and React Native
Difference between React JS and React NativeDifference between React JS and React Native
Difference between React JS and React Native
 
Introduction to React Language (1).pptx
Introduction to React Language  (1).pptxIntroduction to React Language  (1).pptx
Introduction to React Language (1).pptx
 
Java Ide Day 2008 - Presentation on JDeveloper by Paolo Ramasso
Java Ide Day 2008 - Presentation on JDeveloper by Paolo RamassoJava Ide Day 2008 - Presentation on JDeveloper by Paolo Ramasso
Java Ide Day 2008 - Presentation on JDeveloper by Paolo Ramasso
 
Backbone.js with React Views - Server Rendering, Virtual DOM, and More!
Backbone.js with React Views - Server Rendering, Virtual DOM, and More!Backbone.js with React Views - Server Rendering, Virtual DOM, and More!
Backbone.js with React Views - Server Rendering, Virtual DOM, and More!
 
Beginning java ee_7
Beginning java ee_7Beginning java ee_7
Beginning java ee_7
 
Development of web apps based on JSF (TU Vienna)
Development of web apps based on JSF (TU Vienna)Development of web apps based on JSF (TU Vienna)
Development of web apps based on JSF (TU Vienna)
 
React JS - A quick introduction tutorial
React JS - A quick introduction tutorialReact JS - A quick introduction tutorial
React JS - A quick introduction tutorial
 
What Java Programmers Should Learn in 2022.pdf
What Java Programmers Should Learn in 2022.pdfWhat Java Programmers Should Learn in 2022.pdf
What Java Programmers Should Learn in 2022.pdf
 
Silicon Valley Code Camp 2011: Play! as you REST
Silicon Valley Code Camp 2011: Play! as you RESTSilicon Valley Code Camp 2011: Play! as you REST
Silicon Valley Code Camp 2011: Play! as you REST
 
React js vs react native a comparative analysis
React js vs react native a comparative analysisReact js vs react native a comparative analysis
React js vs react native a comparative analysis
 
Wakanda - apps.berlin.js - 2012-11-29
Wakanda - apps.berlin.js - 2012-11-29Wakanda - apps.berlin.js - 2012-11-29
Wakanda - apps.berlin.js - 2012-11-29
 
Workshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General OverviewWorkshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General Overview
 
Jsf 2.0 Overview
Jsf 2.0 OverviewJsf 2.0 Overview
Jsf 2.0 Overview
 
The Java EE 7 Platform: Productivity & HTML5 at San Francisco JUG
The Java EE 7 Platform: Productivity & HTML5 at San Francisco JUGThe Java EE 7 Platform: Productivity & HTML5 at San Francisco JUG
The Java EE 7 Platform: Productivity & HTML5 at San Francisco JUG
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

REST API design and construction with Java EE - pages from my work diary

  • 1.
  • 2. REST API DESIGN AND CONSTRUCTION WITH JAVA EE PAGES FROM MY WORK DIARY By Vineet Reynolds L P / @VineetReynolds Senior Software Engineer JBoss Developer Experience, Red Hat
  • 5. A 10 MINUTE GUIDE TO REST Note ­ this is really a 10 minute guide. This is not a deep­dive into Roy Fielding's doctoral dissertation.
  • 6. WHAT IS REST ? REST = REpresentational State Transfer It is an architectural style. It describes the design of a hypermedia system (the web). It intends to conceptualize how a well designed Web application should behave.
  • 7. EXAMPLES GT/vns E   e e t  returns all Events. GT/vns1 E   e e t /  returns the Event with ID 1. P S   e e t  creates a new Event. OT/vns PT/vns1 U   e e t /  updates the Event. D L T   e e t /  deletes the Event. EEE/vns1
  • 8. EXAMPLES Assuming G T / v n s 1 E   e e t /  returns                                   {   "ae  JDO"   nm":"UCN,   "esos     ssin":[           {"ae  Arga"}            nm":"eoer ,           {"ae  Ifnsa"}            nm":"niipn ,           {"ae  OeSit ,            nm":"pnhf"}           {"ae  WlFy             nm":"idl"}                   ] }                               What kind of updates can be made in a PUT ? Session names could be updated. New Sessions could be added. Existing sessions could be deleted.
  • 9. JAX-RS + JPA + EJB : THE BASICS Let's expose a JPA entity as a REST resource.                               @niy Ett pbi ls vn  ulccasEet{   @d   I   @eeaeVlesrtg  eeainyeAT)   Gnrtdau(taey=GnrtoTp.UO   piaeLn d=nl;   rvt ogi  ul   @eso   Vrin   piaeitvrin=0   rvt n eso  ;   @oun   Clm   piaeSrn ae   rvt tignm;   / etr,Stes qas)adhsCd(   /Gtes etr,eul( n ahoe)   ..   . }                           
  • 10. JAX-RS + JPA + EJB : THE BASICS CREATING NEW EVENTS                               PS sml­vn/eteet TP11 OT/ipeeetrs/vnsHT/. Hs:lclot88 ot oahs:00 CnetTp:apiainjo;hre=T­ otn­ye plcto/sncastUF8 .. . {nm""UCN} "ae:JDO"                                                         HT/. 0 rae TP1121Cetd Lcto:ht:/oahs:00sml­vn/eteet/ oain tp/lclot88/ipeeetrs/vns1 .. .                          
  • 11. JAX-RS + JPA + EJB : THE BASICS FETCHING ALL EVENTS                               GT/ipeeetrs/vnsHT/. E sml­vn/eteet TP11 Hs:lclot88 ot oahs:00 Acp:apiainjo,tx/li,** cet plcto/sn etpan / .. .                                                         HT/. 0 K TP1120O CnetTp:apiainjo otn­ye plcto/sn .. . ["d:,vrin:,nm""UCN} {i"1"eso"0"ae:JDO"]                          
  • 12. JAX-RS + JPA + EJB : THE BASICS FETCHING ONE SPECIFIC EVENT                               GT/ipeeetrs/vns1HT/. E sml­vn/eteet/ TP11 Hs:lclot88 ot oahs:00 Acp:apiainjo,tx/li,** cet plcto/sn etpan / .. .                                                         HT/. 0 K TP1120O CnetTp:apiainjo otn­ye plcto/sn .. . {i"1"eso"0"ae:JDO" "d:,vrin:,nm""UCN}                          
  • 13. JAX-RS + JPA + EJB : THE BASICS UPDATING AN EVENT                               PT/ipeeetrs/vns1HT/. U sml­vn/eteet/ TP11 Hs:lclot88 ot oahs:00 Acp:apiainjo,tx/li,** cet plcto/sn etpan / .. . {i"1"eso"0"ae:JDO ni" "d:,vrin:,nm""UCNIda}                                                         HT/. 0 oCnet TP1124N otn .. .                          
  • 14. JAX-RS + JPA + EJB : THE BASICS DELETING AN EVENT                               DLT sml­vn/eteet/ TP11 EEE/ipeeetrs/vns1HT/. Hs:lclot88 ot oahs:00 Acp:apiainjo,tx/li,** cet plcto/sn etpan / .. .                                                         HT/. 0 oCnet TP1124N otn .. .                          
  • 15. COMPOSITE KEYS ARE NOT FUN Addressability suffers. Matrix parameters will help with composite keys.                           / e o vn  ae oain /Kyfreet=nm,lcto GT/vnsnm=UCNlcto=ni E eet;aeJDO;oainIda                       But ... They're not a standard. Just a W3C design note. Lack of client support (but improving). Among popular JS libraries, only Dojo, AngularJS, Backbone.js has support. If cheap, establish a mapping scheme for composite keys.
  • 17. CREATE A NEW EVENT                               PS bd­vn/eteet TP11 OT/iieetrs/vnsHT/. Hs:lclot88 ot oahs:00 Acp:apiainjo,tx/li,** cet plcto/sn etpan / .. . {nm""UCN,lcto""aglr" "ae:JDO""oain:Bnaoe}                                                         HT/. 0 rae TP1121Cetd Lcto:ht:/oahs:00bd­vn/eteet/ oain tp/lclot88/iieetrs/vns1 .. .                          
  • 18. FETCH AN EXISTING EVENT                               GT/iieetrs/vns1HT/. E bd­vn/eteet/ TP11 Hs:lclot88 ot oahs:00 Acp:apiainjo,tx/li,** cet plcto/sn etpan / .. .                                                         HT/. 0 K TP1120O CnetTp:apiainjo otn­ye plcto/sn .. . {i"1"eso"0"ae:JDO""oain:Bnaoe,ssin"[} "d:,vrin:,nm""UCN,lcto""aglr""esos:]                          
  • 19. CREATE A NEW SESSION                               PS bd­vn/etssin TP11 OT/iieetrs/esosHT/. Hs:lclot88 ot oahs:00 Acp:apiainjo,tx/li,** cet plcto/sn etpan / {   "esoNm""eoer,   ssinae:Arga"   "pae""eata"   sekr:Sbsin,   "vn"{i"1   eet:"d:,       "eso"0       vrin:,       "ae:JDO"       nm""UCN,       "oain:Bnaoe,       lcto""aglr"       "esos:]       ssin"[} }                                                         HT/. 0 rae TP1121Cetd Lcto:ht:/oahs:00bd­vn/etssin/ oain tp/lclot88/iieetrs/esos2 .. .                          
  • 20. FETCH THE NEWLY CREATED SESSION                               GT/iieetrs/esos2HT/. E bd­vn/etssin/ TP11 Hs:lclot88 ot oahs:00 Acp:apiainjo,tx/li,** cet plcto/sn etpan / .. .                                                         HT/. 0 nenlSre ro TP1150Itra evrErr CnetTp:tx/tlcastuf8 otn­ye ethm;hre=t­ JWB005 TPSau 0  r.oeasjcsnmpJoMpigxeto BE006:HT tts50­ogcdhu.ako.a.snapnEcpi fie olzl ntaieacleto frl:cmeapebdeetmd aldt aiyiiilz  olcino oe o.xml.iivn.o n eso truhrfrnecan o.xml.iivn.oe.eso[e oSsin(hog eeec hi:cmeapebdeetmdlSsin"                          
  • 21. WHAT JUST HAPPENED? E e t s s i n  is lazily fetched. vn.esos During serialization, the proxy does not find an active session. Let's try and solve this crudely, using eager fetching ...                               {i"2"eso"0"esoNm""eoer,sekr:Sbsin,eet: "d:,vrin:,ssinae:Arga""pae""eata""vn" ["d:,vrin:,ssinae:Arga""pae""eata""vn" {i"2"eso"0"esoNm""eoer,sekr:Sbsin,eet ["d:,vrin:,ssinae:Arga""pae""eata""vn" {i"2"eso"0"esoNm""eoer,sekr:Sbsin,eet .. . / hsnvrtriae /Ti ee emnts                          
  • 22. HOW DO YOU SOLVE THIS ? If you can, do not expose collection members of JPA entities directly. Use sub­resources for dependent collections (1:M). Use a mapping layer. Create Representation classes to map JPA object graphs. The mappings translate RESTful actions into operations in the domain model. Or, use different representations of the same JPA entity for different operations (not a good idea). One to create an entity with collections, another to update only the entity. Model collections as a separate resource. Makes sense for M:N relationships.
  • 23. JAVA.SQL.* HATES YOU Consider the following                                   @eprlTmoaTp.IE Tmoa(eprlyeTM) jv.tlDt trTm; aaui.aesatie                               During serialization, s a t i e t r T m 's type is j v . q . i e aaslTm. Given a value of 2:00 PM IST, the field will be serialized as {satie:1:00". "trTm""40:0} When deserializing back, { s a t i e : 1 7 ­ 1 "trTm""900­ 0T40:0000:0} 1 1 : 0 0 . 0 + 5 3 "  should be supplied.
  • 26. BEAN VALIDATION AND VIOLATION REPORTING Responses for Constraint violation errors on JPA entities are not standardized. By default, HTML responses will be provided if no E c p i n a p r  exist for xetoMpes CntanVoainxetos osritiltoEcpin. Bean Validation for request parameters is available out­of­ the­box, but requires activation. RESTEasy has one format, Jersey has another.