Rest with java (jax rs) and jersey and swagger

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

Transcript of 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

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.

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

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

Representationsconstrainedi.e., XML

Verbsconstrainedi.e., GET

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 .

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

Representations (MediaType) XML

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

</COURSE>

JSON

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

}

Why it is called “REST” ?

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 .

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 .

Big Picture

Big Picture (contd…)

Your first Jersey project

Your first Jersey project (contd…)

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

Your first Jersey project (contd…)

Your first Jersey project (contd…)

Your first Jersey project (contd…)

Your first Jersey project (contd…)

Root resource class

@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.

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

@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"

@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.

@Consumes

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

CRUD App example (Model)

CRUD App example (Model)

CRUD App example (Database)

CRUD App example (Service)

Crud App (Resource)

RESTFul APIs with 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

Swagger Specification• Resource Listing• API Description

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

specification• Manually - writing the JSON by hand

Swagger Specification

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/