Professional Documents
Culture Documents
JavaLounge
Made Easy
Mario Goller (ZH-AD)
10.05.2012
BASEL
BERN
LAUSANNE
ZRICH
2011 Trivadis
Java Web Services
27.04.2012
DSSELDORF
FRANKFURT A.M.
FREIBURG I.BR.
HAMBURG
MNCHEN
STUTTGART
WIEN
AGENDA
1. Web Service Basics
2. JAX-WS
Overview
Endpoint implementation
Service consumer
3. JAX-RS
Overview
Resources and Parameters
Representations
2011 Trivadis
Java Web Services
27.04.2012
2011 Trivadis
Java Web Services
27.04.2012
SOAP
specifies exactly how to encode an XML file,
i.e. how an application can call another
application and pass information to it
It also specifies how the called application
can return a response
It is fundamentally a stateless one-way
transmission protocol
4
2011 Trivadis
Java Web Services
27.04.2012
AGENDA
1. Web Service Basics
2. JAX-WS
Overview
Endpoint implementation
Service consumer
3. JAX-RS
Overview
Resources and Parameters
Representations
2011 Trivadis
Java Web Services
27.04.2012
Overview JAX-WS
Java API for XML Web Services
Since Java EE 5, JAX-WS (JSR 224) , now also included in Java 6 SE)
replaces the older JAX-RPC API (= old Java web services, basically RMI)
JAX-WS is platform independent (Java frameworks like Metro/Glassfish,
Axis2 or CXF support JAX-WS). Services developed on one platform can
be easily ported to another platform.
Annotation based
Tools available for WSDL consumption
Development Approaches
Contract First (Top Down)
Code First (Bottom Up)
6
2011 Trivadis
Java Web Services
27.04.2012
WSIT
JAX-WS
XML
Transport
7
Security
ReliableMessaging
Transactions
Metadata
WSDL
Policy
HTTP
2011 Trivadis
Java Web Services
27.04.2012
TCP
SMTP
2011 Trivadis
Java Web Services
27.04.2012
JAX-WS Annotation
The @javax.jws.WebService annotation marks a Java class or
interface as being a web service
By default, all the public methods of a web service are exposed in the
WSDL and use all the default mapping rules. To customize some
elements of this mapping, you can apply the @javax.jws.WebMethod
annotation on methods.
The @javax.jws.WebResult annotation operates in conjunction
with @WebMethod to control the generated name of the message
returned value in the WSDL
The @javax.jws.WebParam annotation is similar to @WebResult as it
customizes the parameters for the web service methods.
2011 Trivadis
Java Web Services
27.04.2012
@Stateless
@WebService
public class OrderEJB {
...
}
@WebMethod
public String getOrder(@WebParam(name = "id") int i) {...}
@WebMethod
public @WebResult(name="orderStatus") int getStatus(int i)
{...}
@PostConstruct
public void init() {...}
@PreDestroy
public void teardown() {...}
}
10
2011 Trivadis
Java Web Services
27.04.2012
lifecycle callback
methods
11
2011 Trivadis
Java Web Services
27.04.2012
12
2011 Trivadis
Java Web Services
27.04.2012
This populates the client subdirectory with .class and .java files.
Call the web service using the generated client-side stubs:
13
2011 Trivadis
Java Web Services
27.04.2012
14
2011 Trivadis
Java Web Services
27.04.2012
2011 Trivadis
Java Web Services
27.04.2012
16
2011 Trivadis
Java Web Services
27.04.2012
17
2011 Trivadis
Java Web Services
27.04.2012
JBoss:
uses CXF or Metro as WS-stack (http://www.jboss.org/jbossws)
18
2011 Trivadis
Java Web Services
27.04.2012
JAX-WS Handler
JAX-WS provides a handler framework that allows application code to
inspect and manipulate outgoing and incoming SOAP messages:
LogicalHandler access only to the message payload
SOAPHandler access to the entire SOAP message, including any optional
headers and attachments
1.
2.
19
2011 Trivadis
Java Web Services
27.04.2012
AGENDA
1. Web Service Basics
2. JAX-WS
Overview
Endpoint implementation
Service consumer
3. JAX-RS
Overview
Resources and Parameters
Representations
20
2011 Trivadis
Java Web Services
27.04.2012
What is REST?
REpresentational State Transfer (REST)
REST is an architectural style
Client/server + Request/response approach
21
2011 Trivadis
Java Web Services
27.04.2012
JAX-RS Introduction
Java API for RESTful Web Services (JAX-RS) JSR 311
Standard annotaions-driven API, standard part of Java EE 6
uses annotations on POJOs (Plain Old Java Objects) to map to the
RESTful style of presenting web applications
Jersey reference implementation
Download it from http://jersey.dev.java.net
Comes with Glassfish, Java EE 6
Tools support in NetBeans
22
2011 Trivadis
Java Web Services
27.04.2012
23
2011 Trivadis
Java Web Services
27.04.2012
2011 Trivadis
Java Web Services
27.04.2012
How it works
Application
GET /books/all HTTP/1.1
Accept: text/xml
URL
Matching
Resource
Method
Matching
Content
type
Matching
@GET
@Produces("application/xml")
public List<Book> getAll(){
HTTP/1.1 200 OK
Content-Type: text/xml
25
2011 Trivadis
Java Web Services
27.04.2012
26
2011 Trivadis
Java Web Services
27.04.2012
Server
Client
List all available books
@Path("/books")
public class OrderResource {
@GET
String getAll() {...}
}
GET /books/?id=234
Server
Client
Returns the book with ID 234
@Path("/books")
public class BookResource {
@GET
Book getBook(@QueryParam("id") String id) {...}
}
27
2011 Trivadis
Java Web Services
27.04.2012
HTTP Example
Request
GET /books/all HTTP/1.1
Host: rest.example.com
Accept: application/xml
Method
Resource
Response
HTTP/1.1 200 OK
Date: Tue, 08 May 2012 16:41:58 GMT
Server: Apache/1.3.6
Content-Type: application/xml; charset=UTF-8
<?xml version="1.0"?>
<books xmlns="">
<book></book>
</book>
28
2011 Trivadis
Java Web Services
27.04.2012
Representation
JAX-RS Representations
Static and dynamic content negotiation
Multiple representations
Annotate methods or classes:
@Produces matches Accepts header
@Consumes matches Content-Type header
@GET
@Consumes("application/xml")
@Produces({"application/xml","application/json"})
String getOrder(@PathParam("order_id") String id) {
...
}
29
2011 Trivadis
Java Web Services
27.04.2012
Multiple Representations
Representation oriented
Representations
{order: {id:123, items:[{item: {...}]}}
JSON
GET http://host/orders/123
Order 123
Accept: application/json
Accept-Language: en
XML
30
2011 Trivadis
Java Web Services
27.04.2012
JAX-RS Providers
Provides adaptation between the HTTP world and your own
application domain:
MessageBodyReader
MessageBodyWriter
@Produces("text/xml")
@Provider
public class OrderXMLWriter implements
MessageBodyWriter<List<Order>> {
...
}
31
2011 Trivadis
Java Web Services
27.04.2012
Summary
JAX-WS
easier to use and more powerful than JAX-RPC
annotation-based
part of the Java EE (5/6) and Java SE 6 platforms
Layered design hides the complexity
- Extensible at the protocol and transport level
JAX-RS
High-level declarative programming model for REST
annotation-based
32
2011 Trivadis
Java Web Services
27.04.2012
Summary
Use REST (JAX-RS):
only synchronous
short-running calls
When the web
services are
completely stateless
When bandwidth is
particularly important
and needs to be
limited
33
2011 Trivadis
Java Web Services
27.04.2012
Questions ?
34
2011 Trivadis
Java Web Services
27.04.2012
Trivadis AG
THANK YOU.
Mario Goller
Europastrasse 5
CH-8152 Glattbrugg (ZH)
Tel. +41-44-808 70 20
Fax +41-44-808 70 21
info@trivadis.com
www.trivadis.com
BASEL
35
BERN
LAUSANNE
ZRICH
2011 Trivadis
Java Web Services
27.04.2012
DSSELDORF
FRANKFURT A.M.
FREIBURG I.BR.
HAMBURG
MNCHEN
STUTTGART
WIEN