RESTful with JAX-RS

HOM,DOS,SBK

Intro

Motivation RESTful java with JAX-RS Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling curl Pieter van den Hombergh Tooling curl

Thijs Dorssers Basic understanding Stefan Sobek Uniform constrained interface JAX-RS Fontys Hogeschool voor Techniek en Logistiek JAX-RS Service April 12, 2018 Service Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 1/27 RESTful java with Topics JAX-RS HOM,DOS,SBK

Motivation Intro

Look back at the client-server socket example Motivation Sockets and socket exercise API’s everywhere. reviewed API’s everywhere. Tooling Tooling API console Basics Tooling Netbeans Tooling curl Example Tooling curl Basic Exercise understanding Uniform constrained interface JAX-RS

JAX-RS Service Service

Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 2/27 RESTful java with Motivation JAX-RS HOM,DOS,SBK

To build distributed services. Intro To use the web to realize access to data sources. Motivation Sockets and socket exercise Divide and conquer simplifies complex systems. reviewed API’s everywhere. Tooling API console Replaces complex solutions like SOAP, CORBA and Java RMI. Tooling Netbeans Tooling curl It is a modern protocol that is used in contemporary websites and in Tooling curl communication between apps and servers. Basic understanding Uniform constrained interface JAX-RS

JAX-RS Service Service

Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 3/27 RESTful java with Sockets reviewed JAX-RS HOM,DOS,SBK Server and client are applications which both own a socket. Intro

Motivation Sockets and socket exercise 1 ServerSocket Socket reviewed 2 API’s everywhere. Connection request Tooling API console

P Listen P Tooling Netbeans O O Tooling curl Server Client R R Tooling curl T T Basic understanding

3 Accept… Uniform constrained interface JAX-RS Listen P 4 P JAX-RS O O Server Client Service R R Service T T Protocol Socket Be Lazy, Not Slow

Enabling postgres in payara Exercise Simple RESTapi like protocol: More on PostgreSQL Exercise https://java2.fontysvenlo.org/pdf/simplerestapiprotocol.pdf Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 4/27 RESTful java with Some nice applications of RESTful api’s in the evil outside world. JAX-RS HOM,DOS,SBK Youtube video Published on Jul 14, 2014. This video introduces the viewer to some API concepts by making example calls Intro Motivation to Facebook’s Graph API, Google Maps’ API, Instagram’s Media Search API, Sockets and socket exercise reviewed and Twitter’s Status Update API. API’s everywhere. Tooling API console https://www.youtube.com/watch?v=7YcW25PHnAA&feature=youtu.be Tooling Netbeans Apart from the access tokens you have to get familiar with the addressed Tooling curl Tooling curl

subjects. Basic understanding Uniform constrained interface JAX-RS

JAX-RS Service Service

Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 5/27 RESTful java with API console JAX-RS HOM,DOS,SBK In the previous video an api console was used. Instead we are going to use the app, Advanced REST Client, which is based Intro Motivation on the same technology as Atom, VS-Code: electron-js Sockets and socket exercise reviewed installation guide: API’s everywhere. Tooling API console visit https://install.advancedrestclient.com/ and press the install Tooling Netbeans Tooling curl button if you are on Mac or Windows. (You will want the dmg or exe file) Tooling curl

Basic If you are on a Linux (like me) do NOT press the install button, but instead understanding Uniform constrained select Other Platforms and choose the package for you distro. For me, with interface my debian based Ubuntu it is arc-linux-12.1.2-amd64.deb. If you are on JAX-RS JAX-RS Redhat or derivative, choose the appropriate rpm package. Service You will use ARC to test your own RESTful api service. Stack overflow (actually Service Protocol stackexchange) has a rest API: Be Lazy, Not Slow http://api.stackexchange.com/2.2/search?order=desc&sort=activity&tagged=&site= Enabling postgres stackoverflow. in payara More on PostgreSQL

If you use the URL on the command line, make sure to escape the & Exercise

(ampersand) characters, because they otherwise have a special meaning in the Q&A command shell.

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 6/27 RESTful java with and Netbeans JAX-RS HOM,DOS,SBK Like a website needs a webserver to be hosted, RESTful api’s need a so called application server. We are going to use Payara Server version 5. This is the Intro Motivation improved version of Glassfish, which is the reference implementation of the Java Sockets and socket exercise reviewed EE standard. Brief installation guide: API’s everywhere. Tooling API console Download Payara Server from http://www.payara.fish, unzip it in some Tooling Netbeans Tooling curl folder, NOT like “Program Files” in Windows. Path with spaces are Tooling curl problematic. On Linux you would place in /opt so C backslash opt might Basic understanding be a good place too. Uniform constrained interface In Netbeans, select ”Service” tab ->”Servers” JAX-RS JAX-RS If present delete the current GlassFish Server Service Service

Next right click on ”Servers” and select ”Add Server” and find the payara5 Protocol

folder. Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 7/27 RESTful java with Curl JAX-RS HOM,DOS,SBK curl is a command line tool for transfering data with URL’s and very handy for testing rest api requests. Intro Motivation Mac and linux have curl preinstalled. Window users should google for Sockets and socket exercise reviewed curl-7.40.0-openssl-libssh2-zlib-x64. API’s everywhere. Tooling API console Take https://sourceforge.net/ to download a copy. Tooling Netbeans Tooling curl Tooling curl

Basic understanding Uniform constrained interface JAX-RS

JAX-RS Service Service

Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 8/27 RESTful java with Friendlier CURL: Meet HTTPIE JAX-RS HOM,DOS,SBK More user friendly then curl is httpie. I will demo that. You can find it at https://httpie.org/ Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling curl Tooling curl

Basic understanding Uniform constrained interface JAX-RS

JAX-RS Service It is probably curl on dreadlocks. Service Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 9/27 RESTful java with curl examples JAX-RS HOM,DOS,SBK skip this frame, target website is not maintained. get only one item: Intro Motivation curl −X GET https dosdemo mybluemix net api CoffeeShops Sockets and socket exercise :// . . / / /1 reviewed API’s everywhere. Tooling API console get all items: Tooling Netbeans Tooling curl curl −X GET https :// dosdemo . mybluemix . net / api / CoffeeShops Tooling curl Basic understanding post an item: Uniform constrained interface JAX-RS curl −XPOST −−header ' Content−Type : application /json ' \ −−header ' Accept : application /json ' \ JAX-RS −d { "name" "AB" "city" "Venlo" "country" "NL" } \ ' : , : , : ' Service https :// dosdemo . mybluemix . net / api / CoffeeShops Service

Protocol

Curl is the standard. Same for wget Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 10/27 RESTful java with Basic understanding JAX-RS HOM,DOS,SBK The architectural principles of REpresentational State Transfer are defined as below in Roy Fielding’s PhD thesis: Intro Motivation Addressable resources, addressable through a specific URI. Sockets and socket exercise reviewed API’s everywhere. A uniform constrained interface Tooling API console Tooling Netbeans Representation-oriented: Representations are exchanged between client and Tooling curl service, think about current state of a resource. Format of this Tooling curl Basic representations could be XML, JSON etc. understanding Uniform constrained Communicate statelessly, no client session data stored on the server. interface JAX-RS Hypermedia As The Engine Of Application State (HATEOAS) JAX-RS Service Service

Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 11/27 RESTful java with Uniform constrained interface JAX-RS HOM,DOS,SBK Which sticks to a small set of operations, the most important are: GET(like -select) safe and idempotent Intro Motivation Sockets and socket exercise PUT(like sql-update) idempotent reviewed API’s everywhere. DELETE(like sql-delete) idempotent Tooling API console Tooling Netbeans POST(like sql-insert) not idempotent Tooling curl Tooling curl

Idempotent means that no matter how many times you apply the operation, Basic understanding the result is always the same. Uniform constrained interface Safe means invoking does not change the state of the server. JAX-RS

JAX-RS Service Service

Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 12/27 RESTful java with JAX-RS JAX-RS HOM,DOS,SBK JAX-RS is a framework that focuses on applying Java annotations to plain java objects. It has annotations to bind specific URI patterns and HTTP operations Intro Motivation to methods of java classes. Sockets and socket exercise reviewed @PATH API’s everywhere. Tooling API console @POST Tooling Netbeans Tooling curl @GET Tooling curl Basic @DELETE understanding Uniform constrained interface @PUT JAX-RS These annotations will be explaned in the next slides. JAX-RS Service Service

Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 13/27 RESTful java with JAX-RS JAX-RS HOM,DOS,SBK In Netbeans start creating a Java Web - Web Application. Select no frameworks, because we don’t need them, everything we need is Intro Motivation already available because we selected the Glassfish server. Sockets and socket exercise reviewed Your contains at least the ApplicationConfig.java source: API’s everywhere. Tooling API console package service ; Tooling Netbeans Tooling curl Tooling curl import javax . ws . rs . core . Application ; javax ws rs ApplicationPath Basic import . . . ; understanding Uniform constrained @ApplicationPath ( "webresources" ) interface /** Indeed, an empty class.*/ JAX-RS p u b l i c class ApplicationConfig e x t e n d s Application { JAX-RS } Service Service and a service and an entity, see next slides. Protocol Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 14/27 RESTful java with Service JAX-RS HOM,DOS,SBK @Stateless Intro @Path ( "employee" ) p u b l i c class EmployeeREST { Motivation Sockets and socket exercise reviewed @GET API’s everywhere. @Path ( "{id}" ) Tooling API console @Produces { MediaType APPLICATION_JSON } Tooling Netbeans ( . ) Tooling curl p u b l i c Response find ( @PathParam ( "id" ) Integer id ) { Tooling curl } Basic understanding @POST Uniform constrained interface @Consumes ( { MediaType . APPLICATION_JSON } ) JAX-RS p u b l i c Response create ( Employee employee , @Context UriInfo info { JAX-RS ) Service } Service

Protocol @GET @Produces ( { MediaType . APPLICATION_JSON } ) Be Lazy, Not Slow p u b l i c List getAllEmployees () { Enabling postgres } in payara } More on PostgreSQL Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 15/27 RESTful java with POST-GET JAX-RS HOM,DOS,SBK Live demo, source code is available: SimpleRESTful Intro Motivation Sockets and socket exercise EmployeeClient reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling curl Tooling curl

Basic understanding Uniform constrained interface JAX-RS

JAX-RS Service Service

Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 16/27 RESTful java with Server response JAX-RS HOM,DOS,SBK Response codes examples: 200 OK Intro Motivation Sockets and socket exercise 201 created reviewed API’s everywhere. 404 not found Tooling API console Tooling Netbeans See for more codes: Tooling curl Tooling curl

a comprehensive list: Basic understanding http://www.restpatterns.org/HTTP_Status_Codes Uniform constrained interface a brief list: JAX-RS http://www.restapitutorial.com/lessons/httpmethods.html JAX-RS Service Apart from the code a response mostly contains more. For example, the POST Service response must contain the created entity. In JAX-RS the Resonse class has a set Protocol Be Lazy, Not Slow of methods to help the programmer in building a response object. So study the Enabling postgres Response class documentation and the link above. in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 17/27 RESTful java with Only Java Object when you need them JAX-RS HOM,DOS,SBK Most examples on the net demoing a Intro restful service with Java show it using Java Motivation Business Objects (PoJo) like in the Sockets and socket exercise reviewed previous examples. API’s everywhere. Tooling API console Tooling Netbeans However even more applications are largely Tooling curl CRUD, backed by a database. Tooling curl Basic So the examples degrade to understanding Uniform constrained 1 Get the records from the database interface JAX-RS 2 Turn them into PoJos JAX-RS 3 Have Jax-RS turn the PoJos into JSON Service and send that to the client Service 4 On the way back: the same, but in reverse Protocol order. Be Lazy, Not Slow Enabling postgres even in the case of generated code, you are in payara burning useless cpu-cycles. More on PostgreSQL Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 18/27 RESTful java with Even worse. . . JAX-RS HOM,DOS,SBK

Typically the business object are fetched Intro Motivation with a query per type and then joined in Sockets and socket exercise reviewed the Java application. API’s everywhere. Tooling API console So if you have 5 types, you would have 5 Tooling Netbeans queries equalling 5 round trips. Tooling curl Tooling curl

The database typically can do this in one Basic statement using the appropriate joins. understanding Uniform constrained Ergo: one round trip for all you need on interface JAX-RS your page. JAX-RS Service Service

Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 19/27 RESTful java with So: Let someone else do the work JAX-RS HOM,DOS,SBK Like the database. Intro

PostgreSQL not only understands JSON as a type but also has functions Motivation Sockets and socket exercise to turn records or arrays of records (as in full result set) into a JSON reviewed API’s everywhere. object. Your “Business Layer” only has to forward this as a string. Tooling API console Tooling Netbeans You can still use your normal statements, with a trick (CTE). Tooling curl Tooling curl Doing it in the database (actually: doing as much as possible in the Basic database) has the additional advantage that you can exchange the business understanding Uniform constrained and/or presentation layer quite effortless. Think dot-net, Node.js, ruby, php, interface JAX-RS

and python as examples. JAX-RS Service The technique shown is applicable to all these platforms. Service

Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 20/27 1 RESTful java with Commercial JAX-RS HOM,DOS,SBK

Intro

Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling curl Tooling curl

Basic understanding Uniform constrained interface JAX-RS

JAX-RS Service Service

Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A 1Only: there is no money involved.

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 21/27 RESTful java with bag of tricks JAX-RS HOM,DOS,SBK

pack your query in a common table expression (CTE or WITH clause). Intro

Motivation Sockets and socket exercise SQL magic reviewed API’s everywhere. with _t as(select p . ∗ from president p where id=44 ) Tooling API console s e l e c t to_jsonb ( _t ) from _t ; Tooling Netbeans Tooling curl Tooling curl

Basic result (beautified by hand) understanding Uniform constrained interface { JAX-RS "id" : 44 , JAX-RS "name" "TRUMPDJ" : , Service "party" : "REPUBLICAN" , Service "death_age" : null, Protocol "birth_year" : 1946 , "years_served" : 0 , Be Lazy, Not Slow "state_id_born" : 46 Enabling postgres } in payara More on PostgreSQL The Java code that does this, avoiding code repetition (DRY) is in the repo Exercise under lessondemo. Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 22/27 RESTful java with Payara and postgres JAX-RS HOM,DOS,SBK How to enable postgres in glassfish. Remove all the postgres connection pools and data sources created by you Intro Motivation from payara using the admin console Sockets and socket exercise reviewed API’s everywhere. Go to your project in NB and add New Other Glassfish JDBC Tooling API console Connection Pool. Tooling Netbeans    Tooling curl Tooling curl Give a pool name , e.g. president_pool and select “Extract from Existing Basic Connection” and choose your jdbc:postgresql://... connection, than understanding Uniform constrained next next finish interface JAX-RS

Again right click on your project and go to New Other Glassfish JDBC JAX-RS Resource. Select ”Use existing JDBC Pool” and then select the pool name Service    Service

you have created before than next finish. Protocol  Be Lazy, Not Slow Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 23/27 RESTful java with PostgreSQL on the Internet JAX-RS HOM,DOS,SBK Have a look at the link and the youtube link on the Project 2 website. Intro

Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console On range types in postgres and how they can make your planning tasks easy: Tooling Netbeans Tooling curl https://wiki.postgresql.org/images/7/73/Range-types-pgopen-2012.pdf Tooling curl

Basic understanding Uniform constrained interface JAX-RS Features in advanced database you might want to learn and certainly want: JAX-RS Service https://youtu.be/xEDZQtAHpX8 Service Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 24/27 RESTful java with Exercise JAX-RS HOM,DOS,SBK Exercise: Create a RESTful API service for the entity business case President-Marriage, Intro Motivation using of the President’s database. Sockets and socket exercise reviewed alter the table pres_marriage to have a column divorce_year API’s everywhere. Tooling API console The usecase and API should support: add a spouse and update the divorce Tooling Netbeans Tooling curl column of the previous wife. It is a State operation on said table. Tooling curl Basic understanding Uniform constrained interface JAX-RS

JAX-RS Service Service

Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 25/27 RESTful java with Any open issues? JAX-RS HOM,DOS,SBK Not all understood? See: Intro API documentation on: https://docs.oracle.com/javaee/7/api/ Motivation javax/ws/rs/package-summary.html Sockets and socket exercise reviewed API’s everywhere. Because we rather not want to auto generate our rest api, use sources like the Tooling API console one below only as background: Tooling Netbeans Tooling curl https://netbeans.org/kb/docs/websvc/rest.html Tooling curl Basic Book: understanding Uniform constrained interface Burke, B. (2014). RESTful Java with JAX-RS 2.0. Sebastopol, CA: JAX-RS O’Reilly. JAX-RS Service Youtube: Service Simple Restful Payara example: Protocol Be Lazy, Not Slow https://www.youtube.com/watch?v=F26jcZrgGMk Enabling postgres Introduction Developing RESTful APIs with JAX-RS: in payara More on PostgreSQL

https://youtu.be/xkKcdK1u95s Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 26/27 RESTful java with Q and A JAX-RS HOM,DOS,SBK

Intro

Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling curl Questions? Tooling curl Basic Questions or remarks? understanding Uniform constrained interface JAX-RS

JAX-RS Service Service

Protocol

Be Lazy, Not Slow

Enabling postgres in payara More on PostgreSQL

Exercise

Q&A

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS April 12, 2018 27/27