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

Rest with java (jax rs) and jersey and swagger

Apr 14, 2017

Download

Software

Kumaraswamy M
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Rest with java (jax rs) and jersey and swagger

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

Kumaraswamy M (IBM)

REST with Java (JAX-RS) and Jersey

Page 2: Rest with java (jax rs) and jersey and swagger

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: Rest with java (jax rs) and jersey and swagger

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: Rest with java (jax rs) and jersey and swagger

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

Representationsconstrainedi.e., XML

Verbsconstrainedi.e., GET

Page 5: Rest with java (jax rs) and jersey and swagger

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: Rest with java (jax rs) and jersey and swagger

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: Rest with java (jax rs) and jersey and swagger

Representations (MediaType) XML

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

</COURSE>

JSON

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

}

Page 8: Rest with java (jax rs) and jersey and swagger

Why it is called “REST” ?

Page 9: Rest with java (jax rs) and jersey and swagger

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: Rest with java (jax rs) and jersey and swagger

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: Rest with java (jax rs) and jersey and swagger

Big Picture

Page 12: Rest with java (jax rs) and jersey and swagger

Big Picture (contd…)

Page 13: Rest with java (jax rs) and jersey and swagger

Your first Jersey project

Page 14: Rest with java (jax rs) and jersey and swagger

Your first Jersey project (contd…)

Page 15: Rest with java (jax rs) and jersey and swagger

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

Your first Jersey project (contd…)

Page 16: Rest with java (jax rs) and jersey and swagger

Your first Jersey project (contd…)

Page 17: Rest with java (jax rs) and jersey and swagger

Your first Jersey project (contd…)

Page 18: Rest with java (jax rs) and jersey and swagger

Your first Jersey project (contd…)

Page 19: Rest with java (jax rs) and jersey and swagger

Root resource class

Page 20: Rest with java (jax rs) and jersey and swagger

@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: Rest with java (jax rs) and jersey and swagger

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

Page 22: Rest with java (jax rs) and jersey and swagger

@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: Rest with java (jax rs) and jersey and swagger

@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: Rest with java (jax rs) and jersey and swagger

@Consumes

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

Page 25: Rest with java (jax rs) and jersey and swagger

CRUD App example (Model)

Page 26: Rest with java (jax rs) and jersey and swagger

CRUD App example (Model)

Page 27: Rest with java (jax rs) and jersey and swagger

CRUD App example (Database)

Page 28: Rest with java (jax rs) and jersey and swagger

CRUD App example (Service)

Page 29: Rest with java (jax rs) and jersey and swagger

Crud App (Resource)

Page 30: Rest with java (jax rs) and jersey and swagger

RESTFul APIs with Swagger

Page 31: Rest with java (jax rs) and jersey and swagger

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: Rest with java (jax rs) and jersey and swagger

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: Rest with java (jax rs) and jersey and swagger

Swagger Specification

Page 34: Rest with java (jax rs) and jersey and swagger
Page 35: Rest with java (jax rs) and jersey and swagger

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: Rest with java (jax rs) and jersey and swagger
Page 37: Rest with java (jax rs) and jersey and swagger