Developing RESTful Web services with JAX-RS. Sabyasachi Ghosh, Senior Application Engneer Oracle

Similar documents
JAX-RS and Jersey Paul Sandoz

Introduc)on to JAX- RS 3/14/12

Developing Applications for the Java EE 7 Platform 9-2

Session 12. RESTful Services. Lecture Objectives

Apache Wink User Guide

Mobile Computing. Logic and data sharing. REST style for web services. Operation verbs. RESTful Services

Using the Jersey JAX-RS Reference Implementation 11g Release 1 (10.3.6)

Apache Wink 0.1 Feature Set

5.1 Registration and Configuration

Restful Application Development

RESTful -Webservices

Stefan Tilkov innoq Deutschland GmbH REST + JSR 311 Java API for RESTful Web Services

Oracle Fusion Middleware Developing and Securing RESTful Web Services for Oracle WebLogic Server. 12c ( )

COMP REST Programming in Eclipse

SERVICE TECHNOLOGIES

JVA-563. Developing RESTful Services in Java

RESTful Java with JAX-RS

Developing RESTful Services Using JAX-RS

2 Apache Wink Building Blocks

JSR 311: JAX-RS: The Java API for RESTful Web Services

REST Web Services Objektumorientált szoftvertervezés Object-oriented software design

Using SailFin and Eclipse to build SIP-based and RESTful applications

WA2018 Programming REST Web Services with JAX-RS WebLogic 12c / Eclipse. Student Labs. Web Age Solutions Inc.

JAX-RS. Sam Guinea

02267: Software Development of Web Services

The jpos REST tutorial shows you how to use an out-of-the-box binary distribution of jpos to build a REST server that responds to an /echo call.

REST WEB SERVICES IN JAVA EE 6 AND SPRING 3. Srini Penchikala Austin Java User Group March 30, 2010

Oracle Corporation

RESTful SCA with Apache Tuscany

SCA Java binding.rest

Rest Client for MicroProfile. John D. Ament, Andy McCright

REST & JSR 311. Stefan Tilkov, innoq Deutschland GmbH November 2008

ORACLE APPLICATION EXPRESS, ORACLE REST DATA SERVICES, & WEBLOGIC 12C AUTHOR: BRAD GIBSON SENIOR SOLUTIONS ARCHITECT ADVIZEX

JAX-RS: REST, the Java Way. Stefan Tilkov,

RESTful Services. Distributed Enabling Platform

Module 3 Web Component

JAX-WS 2.2 and JAX-RS 1.1 support in WebSphere Application Server Version 8.0 Beta

<Insert Picture Here> Exploring Java EE 6 The Programming Model Explained

Practice 2. SOAP & REST

Session 8. Reading and Reference. en.wikipedia.org/wiki/list_of_http_headers. en.wikipedia.org/wiki/http_status_codes

JAVA COURSES. Empowering Innovation. DN InfoTech Pvt. Ltd. H-151, Sector 63, Noida, UP

CO Java EE 6: Develop Web Services with JAX-WS & JAX-RS

Migrating traditional Java EE applications to mobile

Oracle - Developing Applications for the Java EE 7 Platform Ed 1 (Training On Demand)

Apache Wink Developer Guide. Draft Version. (This document is still under construction)

Java EE Application Assembly & Deployment Packaging Applications, Java EE modules. Model View Controller (MVC)2 Architecture & Packaging EJB Module

RESTEasy. Distributed peace of mind

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

1Z Java EE 6 Web Services Developer Certified Expert Exam Summary Syllabus Questions

Web Service and JAX-RS. Sadegh Aliakbary

Session 9. Introduction to Servlets. Lecture Objectives

Securing REST using Oracle WebService Manager July 2013

Short introduction to REST

PS/2 Web Services

RESTful Web Services. 20-Jan Gordon Dickens Chariot Solutions

Rest Client for MicroProfile. John D. Ament, Andy McCright

Rest Client for MicroProfile. John D. Ament

RESTFUL WEB SERVICES - INTERVIEW QUESTIONS

1.1 Project/Component Working Name GlassFish v3 HTTP Interface

Java Platform, Enterprise Edition 6 with Extensible GlassFish Application Server v3

Contents at a Glance

Developing Applications with Java EE 6 on WebLogic Server 12c

Java EE 7 is ready What to do next? Peter Doschkinow Senior Java Architect

Session 9. Deployment Descriptor Http. Reading and Reference. en.wikipedia.org/wiki/http. en.wikipedia.org/wiki/list_of_http_headers

Mastering Spring MVC 3

Create and Secure Your REST APIs with Apache CXF

RESTful Java Web Services

Restlet in Action. Developing RESTful web APIs injava JEROME LOUVEL MANNING THIERRY TEMPLIER THIERRY BOILEAU. Shelter Island

JBoss SOAP Web Services User Guide. Version: M5

HTTP Console Documentation

Making applica+ons with Java. Text Technologies 2013 Chris Culy

Developing Enterprise Services for Mobile Devices using Rational Software Architect / Worklight

Session 8. Introduction to Servlets. Semester Project

Courses For Event Java Advanced Summer Training 2018

web.xml Deployment Descriptor Elements

Example jsf-cdi-and-ejb can be browsed at

INTRODUCTION TO COMPONENT DESIGN IN JAVA EE COMPONENT VS. OBJECT, JAVA EE JAVA EE DEMO. Tomas Cerny, Software Engineering, FEE, CTU in Prague,

Understanding RESTful APIs and documenting them with Swagger. Presented by: Tanya Perelmuter Date: 06/18/2018

Unraveling the Mysteries of J2EE Web Application Communications

Java SE7 Fundamentals

Web Application Development Using JEE, Enterprise JavaBeans and JPA

PHP Development for ClearPath. Session 3028, Tuesday, May 15, 2012, 10:30AM Ron Neubauer, Principal Engineer, Unisys Corporation

Java- EE Web Application Development with Enterprise JavaBeans and Web Services

IT Certification Exams Provider! Weofferfreeupdateserviceforoneyear! h ps://

Adam Retter. RESTful XQuery. XQuery 3.0 Annotations for REST. XQuery 3.0 Annotations for REST

Creating RESTful web services with Spring Boot

Black Box DCX3000 / DCX1000 Using the API

The DataNucleus REST API provides a RESTful interface to persist JSON objects to the datastore. All entities are accessed, queried and stored as

CO Java EE 7: Back-End Server Application Development

Java J Course Outline

Session 13. RESTful Services Part 2. Lecture Objectives

Java EE 7: Back-End Server Application Development

Rest Client for MicroProfile. John D. Ament, Andy McCright

Microservices mit Java, Spring Boot & Spring Cloud. Eberhard Wolff

COURSE DETAILS: CORE AND ADVANCE JAVA Core Java

How to implement an OSLC Provider with OSLC4J

com Spring + Spring-MVC + Spring-Boot + Design Pattern + XML + JMS Hibernate + Struts + Web Services = 8000/-

CMP 436/774. Introduction to Java Enterprise Edition. Java Enterprise Edition

Java EE Architecture, Part One. Java EE architecture, part one 1(104)

RESTful Examples. RESTful Examples 1 Apache 3 Jersey 5 JAX-RS 7 ApplicationConfig.java 7 HelloGregg.java 8

Transcription:

Developing RESTful Web services with JAX-RS Sabyasachi Ghosh, Senior Application Engneer Oracle India, @neilghosh

Java API for RESTful Web Services (JAX-RS) Standard annotation-driven API that aims to help developers build RESTful Web services in Java 2

RESTful Application Cycle Resources are identified by URIs Clients communicate with resources via requests using a standard set of methods Requests and responses contain resource representations in formats identified by media types Responses contain URIs that link to further resources 3

Give everything an ID Standard set of methods Link things together Multiple representations Stateless communications 4

ID is a URI http://example.com/widgets/foo http://example.com/customers/bar http://example.com/customers/bar/orders/2 http://example.com/orders/101230/customer 5

Resources are identified by URIs Resource == Java class POJO No required interfaces ID provided by @Path annotation Value is relative URI, base URI is provided by deployment context or parent resource Embedded parameters for non-fixed parts of the URI Annotate class or sub-resource locator method 6

Resources are identified by URIs @Path("orders/{order_id}") public class OrderResource { @GET @Path("customer") CustomerResource getcustomer(@pathparam( order_id )int id) {...} } 7

Standard Set of Methods Method GET POST PUT DELETE Purpose Read, possibly cached Update or create without a known ID Update or create with a known ID Remove 8

Standard Set of Methods Annotate resource class methods with standard method @GET, @PUT, @POST, @DELETE, @HEAD @HttpMethod meta-annotation allows extensions, e.g. WebDAV JAX-RS routes request to appropriate resource class and method Flexible method signatures, annotations on parameters specify mapping from request Return value mapped to response 9

Standard Set of Methods @Path("properties/{name}") public class SystemProperty { } @GET Property get(@pathparam("name") String name) {...} @PUT Property set(@pathparam("name") String name, String value) {...} 10

Parameters Template parameters and Regular expressions @Path("customers/{firstnam e}-{lastnam e}") @Path("{id : \\d+}") Matrix Parameters example.cars.com/mercedes/e55;color=black/2006 Query Paremetsrs DELETE /orders/233?cancel=true 11

Multiple Representations Offer data in a variety of formats XML JSON (X)HTML Maximize reach Support content negotiation Accept header GET /foo Accept: application/json URI-based GET /foo.json 12

Content Negotiation: Accept Header Accept: application/xml Accept: application/json;q=1.0, text/xml;q=0.5, application/xml;q=0.5, @GET @Produces({"application/xml","application/json"}) Order getorder(@pathparam("order_id") String id) {... } @GET @Produces("text/plain") String getorder2(@pathparam("order_id") String id) {... } 13

Content Negotiation: URL-based @Path("/orders") public class OrderResource { @Path("{orderId}.xml") @Produces( application/xml ) @GET public Order getorderinxml(@pathparam("orderid") String orderid) { }... @Path("{orderId}.json") @Produces( application/json ) @GET public Order getorderinjson(@pathparam("orderid") String orderid) { }... } 14

Content Negotiation import javax.ws.rs.get; import javax.ws.rs.path; import javax.ws.rs.produces; import javax.ws.rs.pathparam; import javax.inject.inject; import javax.enterprise.context.requestscoped; @Path("/actor/{id}") @RequestScoped public class ActorResource { @Inject DatbaseBean db; } @GET @Produces("application/json") public Actor getactor( @PathParam("id") int id) { return db.findactorbyid(id); } 15

Link Things Together <order self="http://example.com/orders/101230"> <customer ref="http://example.com/customers/bar"> <product ref="http://example.com/products/21034"/> <amount value="1"/> </order> 16

Responses Contain Links HTTP/1.1 201 Created Date: Wed, 03 Jun 2009 16:41:58 GMT Server: Apache/1.3.6 Location: http://example.com/properties/foo Content-Type: application/order+xml Content-Length: 184 <property self="http://example.com/properties/foo"> <parent ref="http://example.com/properties/bar"/> <name>foo</name> <value>1</value> </order> 17

Responses Contain Links UriInfo provides information about deployment context, the request URI and the route to the resource UriBuilder provides facilities to easily construct URIs for resources 18

Responses Contain Links @Context UriInfo i; SystemProperty p =... UriBuilder b = i.getbaseuribuilder(); URI u = b.path(systemproperties.class).path(p.getname()).build(); List<URI> ancestors = i.getmatcheduris(); URI parent = ancestors.get(1); 19

Responses Contain Links UriBuilder builder = UriBuilder.fromPath("/customers/ {id}"); builder.scheme("http").host("{hostname}").queryparam("param={param}"); http://{hostname}/customers/{id}?param={param} UriBuilder clone = builder.clone(); URI uri = clone.build("example.com", "333", "value"); http://example.com/customers/333?param=value 20

Response codes and Exception Successful HTTP : 200 to 399 200 OK 204 No Content Standard HTTP error : 400 to 599 404 Not Found 406 - Not Acceptable 405 - Method Not Allowed java.lang.exception java.lang.runtimeexception javax.ws.rs.webapplicationexception throw new WebApplicationException(Response.Status.NOT_FOUND); 21

Stateless Communications Long lived identifiers Avoid sessions Everything required to process a request contained in the request 22

Deployment JAX-RS application packaged in WAR like a servlet For JAX-RS aware containers web.xml can point to Application subclass For non-jax-rs aware containers web.xml points to implementation-specific Servlet; and an init-param identifies the Application subclass Resource classes and providers can access Servlet request, context, config and response via injection 23

JAX-RS 1.1 Integration with Java EE 6 Servlets 3.0 No or Portable web.xml <web-app> <servlet> <servlet-name>jersey Web Application</servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet.servletcontainer </servlet-class> <init-param> <param-name>javax.ws.rs.application</param-name> <param-value>com.foo.myapplication</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>jersey Web Application</servlet-name> <url-pattern>/resources/*</url-pattern> </servlet-mapping> </web-app> @ApplicationPath( resources ) public class MyApplication extends javax.ws.rs.core.application { } 24

JAX-RS Summary Java API for building RESTful Web Services POJO based Annotation-driven Server-side API HTTP-centric 25

JAX-RS 1.1 Jersey Client-side API Consume HTTP-based RESTful Services Easy-to-use Better than HttpURLConnection! Reuses JAX-RS API Resources and URI are first-class citizens Not part of JAX-RS yet com.sun.jersey.api.client 26

JAX-RS 1.1 Jersey Client API Code Sample Client client = Client.create(); WebResource resource = client.resource(... ); //curl http://example.com/base String s = resource.get(string.class); //curl -HAccept:text/plain http://example.com/base String s = resource. accept( text/plain ). get(string.class); http://blogs.oracle.com/enterprisetechtips/entry/consuming_restful_web_services_with 27

JAX-RS 1.1 WADL Representation of Resources Machine processable description of HTTP-based Applications Generated OOTB for the application <application xmlns="http://research.sun.com/wadl/2006/10"> <doc xmlns:jersey="http://jersey.dev.java.net/" jersey:generatedby="jersey: 1.1.4.1 11/24/2009 01:37 AM"/> <resources base="http://localhost:8080/hellowadl/resources/"> <resource path="generic"> <method id="gettext" name="get"> <response> <representation mediatype="text/plain"/> </response> </method> <method id="puttext" name="put"> <request> <representation mediatype="text/plain"/> </request> </method> </resource> </resources> </application> 28

References oracle.com/javaee download.oracle.com/javaee/6/tutorial/doc/ jersey.java.net jax-ws.java.net/ 29

Demo 30

Thank You Developing RESTful Web services with JAX-RS Sabyasachi Ghosh, Senior Application Engneer Oracle India, @neilghosh