java web services

Post on 21-May-2015

439 Views

Category:

Technology

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Java EE 6 – Web ServiceVladan Pulec

Objectives Overview Distinguish between the two types of web

services RESTful Service & Demo Web Service & Demo

Key Features

• Platform independent• Language independent

• Interoperable across disparate programming languages

• Leveraging existing technologies (HTTP, XML)• Supported in SE and EE version of Java

Benefits

• Flexibility of supporting unknown future client platforms

• Use of HTTP• Easier communication via firewalls and proxies• Flexible to use a variety of transmission

protocols (ie. SMTP)

Disadvantages

• Verbose (can be slower than other middleware technologies)

• Relying on HTTP, the roles are fixed (only one party can use the service of the other).– Service cannot push, client must pull

Web Service Standards

• There are two prevailing types:• Representational State Transfer (REST) – JSR 311• Simple Object Access Protocol (SOAP) – JSR 224

RESTful Web Services

• Representation State Transfer (REST) revolve around resources (candidate, client, etc)

• The state of the resource is captured and transferred using the service

JAX-RS API

• Does not require any specific data format• Often CSV, JSON (JavaScript Object Notation), or

XML• Provides only server-side API• Can be combined with JAXB or any other Java

XML API

Web Service Endpoints

• Remotely executable components that exist on the server and are executed as a result of receiving a web service request.

• Both JAX-RS and JAX-WS can use the following:• Annotated POJOs• Session bean components (Stateless or Singleton

beans only)

JAVA-RS Endpoints

• The end point is:• Annotated class created to provide web service

functionality• Is instantiated per request• Does not require an EJB container

• JAVA-RS will provide a servlet implementation to handle the requests

JAVA-RS Web Endpoints

• Must have the following:– @javax.ws.rs.Path class annotation– Public methods that are annotated with method

designator (ie. @GET)– Uses @Produces and/or @Consumers annotations– Cannot be abstract

• Web.xml must have a web a JAVA-RS servlet configured– Classes with @Path annotation will be handled by

it.

REST Example

1. A resource is given a specific URL (such as http://localhost/clients/adobe)

2. HTTP methods are used to perform operations– HTTP GET – retrieves the resource representation– HTTP POST – adds new element to the resource – HTTP PUT – creates or updates resource– HTTP DELETE – deletes the resource

• Content can be of any MIME Type (text, XML, etc.)

SOAP Web Services

• Simple Object Access Protocol (SOAP)• More complex than REST but provides more

benefits• To implement SOAP client, developer only

needs to know the Web Services Description Language (WSDL) file, which exposes all API information

• Utilizes HTTP request/response and XML

JAX-WS

• Replaces JAX-RPC• Required minimal knowledge of XML or WSDL

to use basic services• Provides both server and client APIs

sd SOAP

Client Serv ice

JAX-WS runtime JAX-WS runtimeSOAP Message

Sample SOAP Request• <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/“

xmlns:xsd="http://www.w3.org/2001/XMLSchema“ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

• <SOAP-ENV:Header>• <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-

1.0.xsd">• <wsse:UsernameToken>• <wsse:Username>123456</wsse:Username>• <wsse:Password>mypassword</wsse:Password>• </wsse:UsernameToken>• <service_attributes ignore_warnings="false" />• </wsse:Security>• </SOAP-ENV:Header>• <SOAP-ENV:Body>• <ser:findByTestCenterID xmlns:ser="http://services.capacity.vue/">• <testCenterID>4</testCenterID>• </ser:findByTestCenterID>• </SOAP-ENV:Body>• </SOAP-ENV:Envelope>

sd SOAP

Envelope

Header

Body

Sample SOAP Response• <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">• <soap:Body>• <ns2:findByTestCenterIDResponse xmlns:ns2="http://services.capacity.vue/">• <workstationSearchResponse>• <testCenter id="4" name="Electronic Systems" tws="-1" />• <workstationRules>• <workstationRule activeRuleCount="1">• <asset assetCategoryCode="Workstation" assetID="53412"• assetName="Test 7" siteID="4" isEnabled="true" />• </workstationRule>• </workstationRules>• </workstationSearchResponse>• </ns2:findByTestCenterIDResponse>• </soap:Body>• </soap:Envelope>

JAVA-WS Endpoints

• Must have the following:– @javax.jws.WebService annotation– Public methods (cannot be final or static) with

@javax.jws.WebMethod annotation– Class cannot be abstract or final– Must have no args constructor

Data Types

• JAX-WS does not contain JAVA to XML binding• Java Architecture for XML Binding (JAXB) is

designed to handle the binding• Basic Java types are supported, complex ones

require JAXB programming

Java APIs for Web Services

JDOM – provides OO Java model of an XML documentJAXP – abstraction of an XML processingJAXB – converts objects to XML schemasJAX-RPC – remote access API (replaced by JAX-WS)JAXR – standard for using UDDI registriesSAAJ – standard for transmitting and parsing SOAP messagesJAX-RS – RESTful APIJAX-WS – high-level web service API

Additional Resources

JSR 224 – Java API for Web Services (JAX-WS)JSR 331 – Java API for Restful Services (JAX-RS)

top related