of 18 /18
RESTful java with 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 Exercise Q&A RESTful java with JAX-RS Pieter van den Hombergh Thijs Dorssers Stefan Sobek Fontys Hogeschool voor Techniek en Logistiek May 15, 2017 HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 1/18

RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

  • Upload
    others

  • View
    17

  • Download
    0

Embed Size (px)

Citation preview

Page 1: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

RESTful java with JAX-RS

Pieter van den HomberghThijs DorssersStefan Sobek

Fontys Hogeschool voor Techniek en Logistiek

May 15, 2017

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 1/18

Page 2: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

Topics

MotivationLook back at the client-server socket exampleAPI’s everywhere.ToolingBasicsExampleExercise

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 2/18

Page 3: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

Motivation

To build distributed services.To use the web to realize access to data sources.Divide and conquer simplifies complex systems.Replaces complex solutions like SOAP, CORBA and Java RMI.It is a modern protocol that is used in contemporary websites and incommunication between apps and servers.

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 3/18

Page 4: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

Sockets reviewedServer and client are applications which both own a socket.

Server Client

PORT

PORT

Connection request

List

en

ServerSocket1

2

Socket

Server Client

PORT

PORT

List

en4

Socket

3 Accept…

Exercise Simple RESTapi like protocol:https://java2.fontysvenlo.org/pdf/simplerestapiprotocol.pdf

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 4/18

Page 5: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

Some nice applications of RESTful api’s in the evil outside world.Youtube video Published on Jul 14, 2014.This video introduces the viewer to some API concepts by making example callsto Facebook’s Graph API, Google Maps’ API, Instagram’s Media Search API,and Twitter’s Status Update API.https://www.youtube.com/watch?v=7YcW25PHnAA&feature=youtu.beApart from the access tokens you have to get familiar with the addressedsubjects.

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 5/18

Page 6: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

API consoleIn the previous video an api console was used.We are gona be using the Google Chrome plugin called ”Advanced REST Client,installation guide:

Open Google ChromeSelect the three Hamburger bulletsSelect More Tools->Extensions->Get more extensionsSearch for: Advanced REST client offered by chromerestclient.comInstall this plugin.

You will use ARC to test your own RESTful api service, like mine:https://dosdemo.mybluemix.net/api/CoffeeShops (short demo), which ishosted in the cloud.

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 6/18

Page 7: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

Application server and NetbeansLike a website needs a webserver to be hosted, RESTful api’s need a so calledapplication server. We are gonna be using the GlassFish compatible PayaraServer which seamless integrates in Netbeans.Brief installation guide:

Download Payara Server from http://www.payara.fish, unzip it and move itto the application folder, like Program Files in Windows.In Netbeans, select ”Service” tab ->”Servers”If present delete the current GlassFish ServerNext right click on ”Servers” and select ”Add Server” and find the payara41folder.

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 7/18

Page 8: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

Curlcurl is a command line tool for transfering data with URL’s and very handy fortesting rest api requests.Mac and linux have curl preinstalled. Window users should google forcurl-7.40.0-openssl-libssh2-zlib-x64.Take https://sourceforge.net/ to download a copy.

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 8/18

Page 9: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

curl examples

get only one item:curl −X GET https : // dosdemo . mybluemix . net/api/ CoffeeShops /1

get all items:curl −X GET https : // dosdemo . mybluemix . net/api/ CoffeeShops

post an item:curl −X POST −−header ' Content−Type : application /json ' \

−−header ' Accept : application /json ' \−d '{ "name" : "AB" , "city" : " Venlo " , " country " : "NL"} ' \

https : // dosdemo . mybluemix . net/api/ CoffeeShops

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 9/18

Page 10: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

Basic understandingThe architectural principles of REpresentational State Transfer are defined asbelow in Roy Fielding’s PhD thesis:

Addressable resources, addressable through a specific URI.A uniform constrained interfaceRepresentation-oriented: Representations are exchanged between client andservice, think about current state of a resource. Format of thisrepresentations could be XML, JSON etc.Communicate statelessly, no client session data stored on the server.Hypermedia As The Engine Of Application State (HATEOAS)

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 10/18

Page 11: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

Uniform constrained interfaceWhich sticks to a small set of operations, the most important are:

GET(like sql-select) safe and idempotentPUT(like sql-update) idempotentDELETE(like sql-delete) idempotentPOST(like sql-insert) non-idempotent

Idempotent means that no matter how many times you apply the operation, theresult is always the same.Safe means invoking does not change the state of the server.

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 11/18

Page 12: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

JAX-RSJAX-RS is a framework that focuses on applying Java annotations to plain javaobjects. It has annotations to bind specific URI patterns and HTTP operationsto methods of java classes.

@PATH@POST@GET@DELETE@PUT

These annotations will be explaned in the next slides.

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 12/18

Page 13: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

JAX-RSIn Netbeans start creating a Java Web - Web Application.Select no frameworks, because we don’t need them, everything we need isalready available because we selected the Glassfish server.Your source code contains at least the ApplicationConfig.java source:package service ;

impor t javax . ws . rs . core . Application ;impor t javax . ws . rs . ApplicationPath ;

@ApplicationPath ( " webresources " )/** Indeed , an empty class . */p u b l i c c l a s s ApplicationConfig e x t e n d s Application {}

and a service and an entity, see next slides.

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 13/18

Page 14: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

Service

@Stateless@Path ( " employee " )p u b l i c c l a s s EmployeeREST {

@GET@Path ( "{id}" )@Produces ( { MediaType . APPLICATION_JSON } )p u b l i c Response find ( @PathParam ( "id" ) Integer id ) {}

@POST@Consumes ( { MediaType . APPLICATION_JSON } )p u b l i c Response create ( Employee employee ,

@Context UriInfo info ) {}

@GET@Produces ( { MediaType . APPLICATION_JSON } )p u b l i c List<Employee> getAllEmployees ( ) {}

}

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 14/18

Page 15: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

POST-GETLive demo, source code is available:

SimpleRESTfulEmployeeClient

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 15/18

Page 16: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

Server responseResponse codes examples:

200 OK201 created404 not found

See for more codes:a comprehensive list:http://www.restpatterns.org/HTTP_Status_Codes

a brief list:http://www.restapitutorial.com/lessons/httpmethods.html

Apart from the code a response mostly contains more. For example, the POSTresponse must contain the created entity. In JAX-RS the Resonse class has a setof methods to help the programmer in building a response object. So study theResponse class documentation and the link above.

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 16/18

Page 17: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

ExerciseExercises:

1 Create a RESTful API service for the entity President of the President’sdatabase. Use in this exercise as storage an in-memory database like theone in the SimpleRESTful example.

2 Connect your API of exercise one to the President postgres databaseinstead of the in-memory database. Use jdbc to access the database. Startwith GET and POST and than proceed with the remaining requests.

3 And of course you must test your code.

HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 17/18

Page 18: RESTful java with JAX-RS · RESTful java with JAX-RS HOM,DOS,SBK Intro Motivation Sockets and socket exercise reviewed API’s everywhere. Tooling API console Tooling Netbeans Tooling

RESTful java withJAX-RS

HOM,DOS,SBK

Intro

MotivationSockets and socket exercisereviewedAPI’s everywhere.Tooling API consoleTooling NetbeansTooling curlTooling curl

BasicunderstandingUniform constrainedinterfaceJAX-RS

JAX-RSServiceService

Protocol

Exercise

Q&A

Any open issues?Not all understood? See:

API documentation on: https://docs.oracle.com/javaee/7/api/javax/ws/rs/package-summary.html

Because we rather not want to auto generate our rest api, use sources like theone below only as background:

https://netbeans.org/kb/docs/websvc/rest.htmlBook:

Burke, B. (2014). RESTful Java with JAX-RS 2.0. Sebastopol, CA:O’Reilly.

Youtube:Simple Restful Payara example:https://www.youtube.com/watch?v=F26jcZrgGMkIntroduction Developing RESTful APIs with JAX-RS:https://youtu.be/xkKcdK1u95s

Questions?Questions or remarks?HOM,DOS,SBK/FHTenL RESTful java with JAX-RS May 15, 2017 18/18