Java Web services

20
Java EE 6 – Web Service Vladan Pulec

Transcript of Java Web services

Page 1: Java Web services

Java EE 6 – Web ServiceVladan Pulec

Page 2: Java Web services

Objectives Overview Distinguish between the two types of web

services RESTful Service & Demo Web Service & Demo

Page 3: Java Web services

Key Features

• Platform independent• Language independent

• Interoperable across disparate programming languages

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

Page 4: Java Web services

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)

Page 5: Java Web services

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

Page 6: Java Web services

Web Service Standards

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

Page 7: Java Web services

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

Page 8: Java Web services

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

Page 9: Java Web services

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)

Page 10: Java Web services

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

Page 11: Java Web services

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.

Page 12: Java Web services

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

Page 13: Java Web services

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

Page 14: Java Web services

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

Page 15: Java Web services

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

Page 16: Java Web services

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>

Page 17: Java Web services

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

Page 18: Java Web services

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

Page 19: Java Web services

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

Page 20: Java Web services

Additional Resources

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