Sagara	
  Gunathunga	
  
JavaEE	
  and	
  RESTful	
  development	
  
•  RESTfull development
•  Introduction to JavaEE
•  Context and Dependency Injection
•  WSO2 Application Server
2	
  
Agenda	
  
3	
  
	
  	
  REST	
  	
  
REpresentational State Transfer
“An architecture style of networked systems”
4	
  
	
  	
  REST	
  	
  
Let’s take an
example
REpresentational State Transfer
An architecture style of networked systems
Premier
Cooperate
Regular
Customer
Care
Answering
Machine
Regular
Customer
Representative
Premier
Customer
Representative
Cooperate
Customer
Representative
•  Example - Customer care system of a bank
- The bank provides a single telephone number
	
  	
  REST	
  	
  
Premier
Cooperate
Regular
Answering
Machine
Regular
Customer
Representative
Premier
Customer
Representative
Cooperate
Customer
Representative
	
  	
  REST	
  	
  
Answering
Machine
Answering
Machine
•  Example - Customer care system of a bank
- The bank provides a multiple(unique) telephone numbers
Premier
Cooperate
Regular
Customer
Care
Answering
Machine
Regular
Customer
Representative
Premier
Customer
Representative
Cooperate
Customer
Representative
•  Example - Customer care system of a bank
- The bank provides a single telephone number
	
  	
  REST	
  	
  
Not
REST
Premier
Cooperate
Regular
Answering
Machine
Regular
Customer
Representative
Premier
Customer
Representative
Cooperate
Customer
Representative
	
  	
  REST	
  	
  
Answering
Machine
Answering
Machine
•  Example - Customer care system of a bank
- The bank provides a multiple(unique) telephone numbers
REST
Resources	
  
Every	
  disAnguishable	
  enAty	
  is	
  a	
  resource.	
  	
  A	
  resource	
  may	
  be	
  a	
  Web	
  site,	
  an	
  
HTML	
  page,	
  an	
  XML	
  document,	
  a	
  Web	
  service,	
  a	
  physical	
  device,	
  etc.	
  
	
  
	
  
URLs	
  Iden4fy	
  Resources	
  
Every	
  resource	
  is	
  uniquely	
  idenAfied	
  by	
  a	
  URL.	
  	
  	
  
9	
  
	
  	
  REST	
  	
  Fundamentals	
  	
  	
  
10	
  
	
  	
  REST	
  HTTP Request/Response	
  
11	
  
	
  	
  REST	
  HTTP Request/Response	
  
12	
  
HTTP Method CRUD Desc.
POST CREATE Create -
GET RETRIEVE Retrieve Safe,Idempotent,Cacheable
PUT UPDATE Update Idempotent
DELETE DELETE Delete Idempotent
	
  	
  REST	
  HTTP Methods 	
  
13	
  
	
  	
  REST	
  	
  Fundamentals	
  	
  	
  
• Client-Server
• Separation principle
• Components Independent
• Stateless
• Session state on the client
• Visibility, reliability and scalability
• Trade off (network performance, etc.)
• Cacheable
• A response can be cacheable
• Efficiency but reduce reliability
• Layered system
• System scalability
• 
14	
  
JAX-­‐RS	
  –	
  Java	
  API	
  for	
  RESTfull	
  	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  WebServices	
  	
  
•  Annotation driven API
•  Expose annotated Java bean as HTTP based services
•  Implementation may support WADL
•  Define JAVA ó HTTP mapping
•  Number of implementations
•  Apache CXF
•  Apache Wink
•  Jersey
JAX-­‐RS	
  
JAX-RS Annotated Service
@Path("/hello”)
public class HelloWorldService {
@GET
@Path("/{user}")
public String hello(@PathParam("user") String user) {
}
}
16	
  
JAX-­‐WS	
  Annota4ons	
  	
  
•  @Path
•  @GET
•  @PUT
•  @POST
•  @DELETE
•  @Produces
•  @Consumes
•  @PathParam
•  @QueryParam
•  @MatrixParam
•  @HeaderParam
•  @CookieParam
•  @FormParam
•  @DefaultValue
•  @Context
JAX-­‐RS	
  
JAX-RS Annotated Service
@Path("/hello”)
public class HelloWorldService {
@GET
@Consumes("text/plain")
@Produces("text/xml")
@Path("/{user}")
public String hello(@PathParam("user") String user) {
}
}
18	
  
JavaEE	
  
19	
  
JavaEE	
  6	
  	
  
20	
  
JavaEE	
  –	
  Example	
  	
  
Student REST Service Student DatabaseClients
Let’s take an
example
21	
  
JAX-RS service to
accept HTTP calls
JavaEE	
  –	
  Example	
  	
  
@Path("/student”)
public class HelloWorldService {
@GET
@Path(”getall")
@Produces({"application/json”})
public String getAll() {
}
}
22	
  
JavaEE	
  –	
  Example	
  	
  
@Stateful
public class StudentManagerImpl implements StudentManager {
@PersistenceContext(unitName = "rest-jpa”)
private EntityManager entityManager;
public Student getStudent(int index) {
Student student = entityManager.find(Student.class, index);
return student;
}
}
JPA based data
access service
23	
  
JavaEE	
  –	
  Example	
  	
  
How to integrate
JAX-RS service
with JPA DAO
service ?
JAX-RS service
JPA service
24	
  
JavaEE	
  –	
  Example	
  integra4on	
  	
  
@Path("/student”)
public class HelloWorldService {
StudentManager sm;
public HelloWorldService() {
sm = new StudentManagerImpl();
}
@GET
@Path(”getall")
@Produces({"application/json”})
public String getAll() {
}
}
How to integrate
JAX-RS service
with JPA DAO
service ?
Pure JAVA
25	
  
JavaEE	
  –	
  Example	
  integra4on	
  	
  
@Path("/student”)
public class HelloWorldService {
StudentManager sm;
public void setStudentManager (StudentManager sm) { }
public StudentManager setStudentManager () { }
@GET
@Path(”getall")
@Produces({"application/json”})
public String getAll() {
}
}
How to integrate
JAX-RS service
with JPA DAO
service ?
Spring
Framework
26	
  
JavaEE	
  –	
  Example	
  integra4on	
  	
  
@Path("/student”)
public class HelloWorldService {
@Inject
StudentManager sm;
@GET
@Path(”getall")
@Produces({"application/json”})
public String getAll() {
}
}
How to integrate
JAX-RS service
with JPA DAO
service ?
JavaEE
CDI
27	
  
JavaEE	
  –	
  Example	
  integra4on	
  	
  
How to integrate
JAX-RS service
with JPA DAO
service ?JAX-RS service
JPA service
CDI
28	
  
JavaEE	
  –	
  Context	
  and	
  Dependency	
  InjecAon	
  
	
  
@Decorator
@Delegate
@ApplicationScoped
@ConversationScoped
@Dependent
@NormalScope
@RequestScoped
@SessionScoped
@Observes
@Alternative
@Any
@Default
@Disposes
@Model
@New
@Produces
@Specializes
@Stereotype
@Typed
@inject
@Named
@Qualifier
@Scope
@Singleton
29	
  
	
  WSO2	
  Applica4on	
  Server	
  	
  
Yet another
app server ?
•  Tenant-aware data sources
•  Tenant-aware JNDI service
•  Tenant-aware session persistence
•  Tenant-aware user store
•  Tenant-aware authentication and
authorization
•  Tenant-aware Caching
•  Tenant-aware configuration
registry
•  Tenant-aware dashboards
•  Tenant-aware management
console
JavaEE	
  –	
  MulA-­‐tenancy	
  support	
  	
  
	
  
MulAple	
  classloading	
  	
  runAmes	
  	
  
There are four in-built environments
•  Tomcat – Only Tomcat libs are visible (Minimal runtime)
•  Carbon – Tomcat + Carbon libs are visible
•  CXF - Tomcat + CXF + Spring are visible
•  Javaee – JavaEE libs are visible
MulAple	
  classloading	
  	
  runAmes	
  	
  
Ability to create your own custom ‘Classloader Runtime environments’
•  Create directory to place your Jar dependencies
•  Add an entry to webappclassloading-
environments.xml file
e.g. –You can have number of incompatible versions of
Spring frameworks in server level
Server-­‐side	
  JavaScript	
  
•  Develop complete server side application using
Jaggery.js and deploy on Application Server
•  Web Applications
•  RESTfull services
•  WebSocket services
In-­‐built	
  API	
  management	
  	
  
JavaEE	
  Web	
  Profile	
  support	
  	
  
 Web	
  Service	
  development	
  	
  
•  Supported web service development
models
•  JAX –WS ( based on Apache CXF)
•  Apache Axis2
•  Bring your own WS framework
•  Metro, Spring WS
•  Rich dashboard and development tools
•  Try-It tool support
•  Download WS client as maven project
•  Basic statistics
•  Service publish and discovery support
through WSO2 Governance Registry
•  WS – Discovery supported
RESTfull	
  service	
  development	
  	
  
•  Supported RESTfull service
development models.
•  JAX –RS ( based on Apache CXF)
•  Apache Axis2 REST services
•  Jaggery REST services ( Java
Script )
• 
•  Bring your own WS framework
•  Jersey, Rest Easy, Restlet, Spring
•  Rich dashboard and development tools
•  Swagger support.
•  Download REST client as maven
project
•  Basic statistics
Web	
  Socket	
  development	
  	
  
•  Supported WebSocket development models
•  Java API for WebSocket
•  Jaggery Web Sockets ( Java Script )
AcAvity	
  and	
  server	
  monitoring	
  	
  
•  Embedded Monitoring dashboard
•  Request, response, errors, sessions over time
•  Geo info , language, referrals
•  Alerts on thresholds
•  Server and infrastructure health monitoring.
AcAvity	
  and	
  server	
  monitoring	
  	
  
41	
  
References
•  https://github.com/wso2as-developer/javaee-samples
•  https://docs.wso2.com/display/AS521/WSO2+Application+Server
+Documentation
•  http://wso2.com/whitepapers/evaluating-java-ee-application-migration-and-java-
ee-service-migration-to-wso2-application-server/
Contact	
  us	
  !	
  

JavaEE and RESTful development - WSO2 Colombo Meetup

  • 1.
    Sagara  Gunathunga   JavaEE  and  RESTful  development  
  • 2.
    •  RESTfull development • Introduction to JavaEE •  Context and Dependency Injection •  WSO2 Application Server 2   Agenda  
  • 3.
    3      REST     REpresentational State Transfer “An architecture style of networked systems”
  • 4.
    4      REST     Let’s take an example REpresentational State Transfer An architecture style of networked systems
  • 5.
  • 6.
    Premier Cooperate Regular Answering Machine Regular Customer Representative Premier Customer Representative Cooperate Customer Representative    REST     Answering Machine Answering Machine •  Example - Customer care system of a bank - The bank provides a multiple(unique) telephone numbers
  • 7.
  • 8.
    Premier Cooperate Regular Answering Machine Regular Customer Representative Premier Customer Representative Cooperate Customer Representative    REST     Answering Machine Answering Machine •  Example - Customer care system of a bank - The bank provides a multiple(unique) telephone numbers REST
  • 9.
    Resources   Every  disAnguishable  enAty  is  a  resource.    A  resource  may  be  a  Web  site,  an   HTML  page,  an  XML  document,  a  Web  service,  a  physical  device,  etc.       URLs  Iden4fy  Resources   Every  resource  is  uniquely  idenAfied  by  a  URL.       9      REST    Fundamentals      
  • 10.
    10      REST  HTTP Request/Response  
  • 11.
    11      REST  HTTP Request/Response  
  • 12.
    12   HTTP MethodCRUD Desc. POST CREATE Create - GET RETRIEVE Retrieve Safe,Idempotent,Cacheable PUT UPDATE Update Idempotent DELETE DELETE Delete Idempotent    REST  HTTP Methods  
  • 13.
    13      REST    Fundamentals       • Client-Server • Separation principle • Components Independent • Stateless • Session state on the client • Visibility, reliability and scalability • Trade off (network performance, etc.) • Cacheable • A response can be cacheable • Efficiency but reduce reliability • Layered system • System scalability • 
  • 14.
    14   JAX-­‐RS  –  Java  API  for  RESTfull                                                            WebServices     •  Annotation driven API •  Expose annotated Java bean as HTTP based services •  Implementation may support WADL •  Define JAVA ó HTTP mapping •  Number of implementations •  Apache CXF •  Apache Wink •  Jersey
  • 15.
    JAX-­‐RS   JAX-RS AnnotatedService @Path("/hello”) public class HelloWorldService { @GET @Path("/{user}") public String hello(@PathParam("user") String user) { } }
  • 16.
    16   JAX-­‐WS  Annota4ons     •  @Path •  @GET •  @PUT •  @POST •  @DELETE •  @Produces •  @Consumes •  @PathParam •  @QueryParam •  @MatrixParam •  @HeaderParam •  @CookieParam •  @FormParam •  @DefaultValue •  @Context
  • 17.
    JAX-­‐RS   JAX-RS AnnotatedService @Path("/hello”) public class HelloWorldService { @GET @Consumes("text/plain") @Produces("text/xml") @Path("/{user}") public String hello(@PathParam("user") String user) { } }
  • 18.
  • 19.
  • 20.
    20   JavaEE  –  Example     Student REST Service Student DatabaseClients Let’s take an example
  • 21.
    21   JAX-RS serviceto accept HTTP calls JavaEE  –  Example     @Path("/student”) public class HelloWorldService { @GET @Path(”getall") @Produces({"application/json”}) public String getAll() { } }
  • 22.
    22   JavaEE  –  Example     @Stateful public class StudentManagerImpl implements StudentManager { @PersistenceContext(unitName = "rest-jpa”) private EntityManager entityManager; public Student getStudent(int index) { Student student = entityManager.find(Student.class, index); return student; } } JPA based data access service
  • 23.
    23   JavaEE  –  Example     How to integrate JAX-RS service with JPA DAO service ? JAX-RS service JPA service
  • 24.
    24   JavaEE  –  Example  integra4on     @Path("/student”) public class HelloWorldService { StudentManager sm; public HelloWorldService() { sm = new StudentManagerImpl(); } @GET @Path(”getall") @Produces({"application/json”}) public String getAll() { } } How to integrate JAX-RS service with JPA DAO service ? Pure JAVA
  • 25.
    25   JavaEE  –  Example  integra4on     @Path("/student”) public class HelloWorldService { StudentManager sm; public void setStudentManager (StudentManager sm) { } public StudentManager setStudentManager () { } @GET @Path(”getall") @Produces({"application/json”}) public String getAll() { } } How to integrate JAX-RS service with JPA DAO service ? Spring Framework
  • 26.
    26   JavaEE  –  Example  integra4on     @Path("/student”) public class HelloWorldService { @Inject StudentManager sm; @GET @Path(”getall") @Produces({"application/json”}) public String getAll() { } } How to integrate JAX-RS service with JPA DAO service ? JavaEE CDI
  • 27.
    27   JavaEE  –  Example  integra4on     How to integrate JAX-RS service with JPA DAO service ?JAX-RS service JPA service CDI
  • 28.
    28   JavaEE  –  Context  and  Dependency  InjecAon     @Decorator @Delegate @ApplicationScoped @ConversationScoped @Dependent @NormalScope @RequestScoped @SessionScoped @Observes @Alternative @Any @Default @Disposes @Model @New @Produces @Specializes @Stereotype @Typed @inject @Named @Qualifier @Scope @Singleton
  • 29.
    29    WSO2  Applica4on  Server     Yet another app server ?
  • 30.
    •  Tenant-aware datasources •  Tenant-aware JNDI service •  Tenant-aware session persistence •  Tenant-aware user store •  Tenant-aware authentication and authorization •  Tenant-aware Caching •  Tenant-aware configuration registry •  Tenant-aware dashboards •  Tenant-aware management console JavaEE  –  MulA-­‐tenancy  support      
  • 31.
    MulAple  classloading    runAmes     There are four in-built environments •  Tomcat – Only Tomcat libs are visible (Minimal runtime) •  Carbon – Tomcat + Carbon libs are visible •  CXF - Tomcat + CXF + Spring are visible •  Javaee – JavaEE libs are visible
  • 32.
    MulAple  classloading    runAmes     Ability to create your own custom ‘Classloader Runtime environments’ •  Create directory to place your Jar dependencies •  Add an entry to webappclassloading- environments.xml file e.g. –You can have number of incompatible versions of Spring frameworks in server level
  • 33.
    Server-­‐side  JavaScript   • Develop complete server side application using Jaggery.js and deploy on Application Server •  Web Applications •  RESTfull services •  WebSocket services
  • 34.
  • 35.
    JavaEE  Web  Profile  support    
  • 36.
     Web  Service  development     •  Supported web service development models •  JAX –WS ( based on Apache CXF) •  Apache Axis2 •  Bring your own WS framework •  Metro, Spring WS •  Rich dashboard and development tools •  Try-It tool support •  Download WS client as maven project •  Basic statistics •  Service publish and discovery support through WSO2 Governance Registry •  WS – Discovery supported
  • 37.
    RESTfull  service  development     •  Supported RESTfull service development models. •  JAX –RS ( based on Apache CXF) •  Apache Axis2 REST services •  Jaggery REST services ( Java Script ) •  •  Bring your own WS framework •  Jersey, Rest Easy, Restlet, Spring •  Rich dashboard and development tools •  Swagger support. •  Download REST client as maven project •  Basic statistics
  • 38.
    Web  Socket  development     •  Supported WebSocket development models •  Java API for WebSocket •  Jaggery Web Sockets ( Java Script )
  • 39.
    AcAvity  and  server  monitoring     •  Embedded Monitoring dashboard •  Request, response, errors, sessions over time •  Geo info , language, referrals •  Alerts on thresholds •  Server and infrastructure health monitoring.
  • 40.
    AcAvity  and  server  monitoring    
  • 41.
    41   References •  https://github.com/wso2as-developer/javaee-samples • https://docs.wso2.com/display/AS521/WSO2+Application+Server +Documentation •  http://wso2.com/whitepapers/evaluating-java-ee-application-migration-and-java- ee-service-migration-to-wso2-application-server/
  • 42.