Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

37
By Neerav Arora (Cerner Healthcare Solutions Pvt Ltd.) & Kumaraswamy M (IBM) REST with Java (JAX-RS) and Jersey

Transcript of Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Page 1: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

By Neerav Arora (Cerner Healthcare Solutions Pvt Ltd.)&

Kumaraswamy M (IBM)

REST with Java (JAX-RS) and Jersey

Page 2: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Agenda

Rest API concepts . Foundation . JAX-RS and Jersey . Setting up Jersey project in eclipse. JAX-RS annotations . CRUD app. Restful API’s with Swagger.

Page 3: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Rest API Concepts

Services exposed to internet for programmatic access Users can be either producers or consumers or both Eg : api.twitter.com Data can be returned in the form of XML /JSON / etc Helps developers to parse data. Messages can be exchanged in any kind of HTTP method

Page 4: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

FoundationNouns (Resources)unconstrainedi.e., http://example.com/employees/12345

Representationsconstrainedi.e., XML

Verbsconstrainedi.e., GET

Page 5: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Resources REST uses URI to identify resources .

http://localhost/books/ http://localhost/books/ISBN-0011 http://localhost/books/ISBN-0011/authors

http://localhost/classes http://localhost/classes/cs2650 http://localhost/classes/cs2650/students

As you traverse the path from more generic to more specific, you are navigating the data .

Page 6: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Action/Verb /orders

GET - list all orders POST - submit a new order

/orders/{order-id} GET - get an order representation PUT - update an order DELETE - cancel an order

/orders/average-sale GET - calculate average sale

/customers GET - list all customers POST - create a new customer

/customers/{cust-id} GET - get a customer representation DELETE- remove a customer

/customers/{cust-id}/orders GET - get the orders of a customer

Page 7: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Representations (MediaType) XML

<COURSE><ID>CS2650</ID><NAME>Distributed Multimedia Software</NAME>

</COURSE>

JSON

{“course”: {“id”: “CS2650”“name”: “Distributed Multimedia Software”}

}

Page 8: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Why it is called “REST” ?

Page 9: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

JAX-RS

Java API for RESTful Web Services . Provides support in creating web services according to the

Representational State Transfer (REST) architectural pattern . Uses annotations .

Page 10: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Jersey

One of the libraries that implements JAX-RS , from Oracle. Other libraries in the market -

* Apache CXF , an open sourceWeb service framework .* RESTeasy, JBoss 's implementation .* Restlet .* Apache Wink ,Apache Software Foundation Incubator .* WebSphere Application Server from IBM.

Choice of library doesn’t matter .

Page 11: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Big Picture

Page 12: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Big Picture (contd…)

Page 13: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Your first Jersey project

Page 14: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Your first Jersey project (contd…)

Page 15: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

http://repo1.maven.org/maven2/archetype-catalog.xml

Your first Jersey project (contd…)

Page 16: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Your first Jersey project (contd…)

Page 17: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Your first Jersey project (contd…)

Page 18: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Your first Jersey project (contd…)

Page 19: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Root resource class

Page 20: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

@PathSets the path to base URL + /your_path. The base URL is based on your application name, the servlet and the URL pattern from the web.xml configuration file.

Page 21: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

@GET, @PUT, @POST, @DELETE, ...

Page 22: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

@Produces@Produces defines which MIME type is delivered by a method annotated with @GET. In the example text ("text/plain") is produced. Other examples would be "application/xml" or "application/json"

Page 23: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

@PathParamUsed to inject values from the URL into a method parameter. This way you inject, for example, the ID of a resource into the method to get the correct object.

Page 24: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

@Consumes

@Consumes defines which MIME type is consumed by this method.

Page 25: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

CRUD App example (Model)

Page 26: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

CRUD App example (Model)

Page 27: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

CRUD App example (Database)

Page 28: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

CRUD App example (Service)

Page 29: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Crud App (Resource)

Page 30: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

RESTFul APIs with Swagger

Page 31: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

What is Swagger and Why Swagger?• Simple representation of your RESTFul API• Declarative resource specification• JSON specification• Swagger UI to interact with the API in a sandbox UI• Why Jersey Client / JUNIT for your client or boss?• Developers and documentation… hahaha• Support your existing RESTFul API without change

Page 32: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Swagger Specification• Resource Listing• API Description

Create Swagger Specification• Codgen - converts annotations in your code into swagger

specification• Manually - writing the JSON by hand

Page 33: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

Swagger Specification

Page 34: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
Page 35: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey

References• http://www.vogella.com/tutorials/REST/article.html• https://jersey.java.net/documentation/latest/jaxrs-

resources.html• https://github.com/koushikkothagal/messenger• http://swagger.io/specification/• https://github.com/swagger-api/swagger-spec• https://github.com/swagger-api/swagger-core• http://petstore.swagger.io/

Page 36: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
Page 37: Eclipse Day India 2015 - Rest with Java (jax rs) and jersey