cmp 436/774 introduction to webservices

19
1 1 CMP 436/774 Introduction to Webservices Refereces Main Reference R1 (chapters 18, 19, 20, 21) Fall 2012 Department of Mathematics and Computer Science Lehman College, CUNY Web Services Web services are client and server applications that communicate over the World Wide Web’s (WWW) HyperText Transfer Protocol (HTTP). Web services provide a standard means of interoperating between software applications running on a variety of platforms and frameworks. (e.g., web service developed in Java may be accessed by clients written in other languages, and vice versa) On the conceptual level, a service is a software component provided through a network-accessible endpoint. The service consumer and provider use messages to exchange invocation request and response information in the form of self-containing XML documents. The two types of web services are JAX-WS: “big” web services based on SOAP (Simple Object Access Protocol) JAX-RS: “RESTful” web services based on REST (Representational State Transfer) protocol. 2

Upload: others

Post on 03-Feb-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMP 436/774 Introduction to Webservices

1

1

CMP 436/774

Introduction to

Webservices Refereces

Main Reference R1 (chapters 18, 19, 20, 21)

Fall 2012

Department of Mathematics

and Computer Science

Lehman College, CUNY

Web Services

Web services are client and server applications that communicate over

the World Wide Web’s (WWW) HyperText Transfer Protocol (HTTP).

Web services provide a standard means of interoperating between

software applications running on a variety of platforms and frameworks.

(e.g., web service developed in Java may be accessed by clients written in other languages, and vice versa)

On the conceptual level, a service is a software component provided

through a network-accessible endpoint. The service consumer and

provider use messages to exchange invocation request and response

information in the form of self-containing XML documents.

The two types of web services are

JAX-WS: “big” web services based on SOAP (Simple Object Access Protocol)

JAX-RS: “RESTful” web services based on REST (Representational State Transfer) protocol.

2

Page 2: CMP 436/774 Introduction to Webservices

2

Web Services with JAX-WS

When using SOAP protocol, web service operations are defined in

an XML document (WSDL: Web Service Description Language)

file.

After creating WSDL, implementation of web services is done in a

Programming Language such as Java.

With Java EE, a WSDL document can be automatically generated from a web service written in Java (when the web service is deployed to the application server).

If we have WSDL file available, and need to be implemented in Java, Java code can be created for the web service operation (described in WSDL).

3

Creating a simple JAX-WS web service

Create a new web application project “UnitConversion”

Performs conversion of units of length

Web service operations: inchesToCentimeters, centiMetersToInches

4

Page 3: CMP 436/774 Introduction to Webservices

3

JAX-WS UnitConversion (1)

Add Web Service (from Web Services category) to UnitConversion

Project

5

JAX-WS UnitConversion (2)

Enter WS name and package name, check create WS from scratch

6

Page 4: CMP 436/774 Introduction to Webservices

4

JAX-WS UnitConversion (3)

Generated Source Code

7

JAX-WS UnitConversion (4)

@WebService annotation: marks the Java class as a web service

E.g., @WebService(serviceName = "UnitConversion")

@WebMethod annotation: the annotated method as a web service

operation

@WebParam: define the properties of the web service operation

parameters

E.g.,

@WebMethod(operationName = "hello")

public String hello(@WebParam(name = "name") String txt) {

return "Hello " + txt + " !";

}

8

Page 5: CMP 436/774 Introduction to Webservices

5

JAX-WS UnitConversion (5)

Use GUI to add/remove web service operations

Click Design button

9

JAX-WS UnitConversion (6)

Design View

10

Page 6: CMP 436/774 Introduction to Webservices

6

JAX-WS UnitConversion (7)

Click hello operation then Click on Remove Operation

Click on Add Operation

11

JAX-WS UnitConversion (8)

12

Page 7: CMP 436/774 Introduction to Webservices

7

JAX-WS UnitConversion (9)-- Generated Code

@WebService(serviceName = "UnitConversion")

public class UnitConversion {

/**

* Web service operation

*/

@WebMethod(operationName = "inchesToCentimeters")

public double inchesToCentimeters(@WebParam(name = "inches") double inches) {

//TODO write your implementation code here:

return 0.0;

}

/**

* Web service operation

*/

@WebMethod(operationName = "centimetersToInches")

public double centimetersToInches(@WebParam(name = "centimeters") double centimeters) {

//TODO write your implementation code here:

return 0.0;

}

}

13

Testing JAX-WS UnitConversion (1)

Build and Deploy, then

14

Page 8: CMP 436/774 Introduction to Webservices

8

JAX-WS UnitConversion (2)

15

16

Page 9: CMP 436/774 Introduction to Webservices

9

17

Develop a JAX-WS Client (1)

Create a Simple Java Application

18

Page 10: CMP 436/774 Introduction to Webservices

10

Develop a JAX-WS Client (2)

19

Develop a JAX-WS Client (3)

Browse the deployed Web Service

20

Page 11: CMP 436/774 Introduction to Webservices

11

Develop a JAX-WS Client (4)

21

Develop a JAX-WS Client (5)

22

Page 12: CMP 436/774 Introduction to Webservices

12

Develop a JAX-WS Client (6)

Dragging the inchesToCentimeters operation from the UnitConversionPort to the

UnitConversionClient.java class.

23

JAX-WS Client (7)

Build and Run the project

24

Page 13: CMP 436/774 Introduction to Webservices

13

JAX-WS Client (7)

25

Exposing EJBs as JAX-WS Web Services

26

Page 14: CMP 436/774 Introduction to Webservices

14

EJBs as JAX-WS (2)

27

EJBs as JAX-WS (3)

@WebService(serviceName = "VolumeUnitConversion")

@Stateless()

public class VolumeUnitConversion {

/**

* Web service operation

*/

@WebMethod(operationName = "litersToGallons")

public double litersToGallons(@WebParam(name = "liters") double liters) {

//TODO write your implementation code here:

return liters * 0.26417;

}

/**

* Web service operation

*/

@WebMethod(operationName = "gallonsToLiters")

public double gallonsToLiters(@WebParam(name = "gallons") double gallons) {

//TODO write your implementation code here:

return gallons * 3.7854;

}

}

28

Page 15: CMP 436/774 Introduction to Webservices

15

29

RESTful Web Services with JAX-RS

Representational State Transfer (REST) is an architectural style in

which web services are viewed as resources and can be identified

by Uniform Resource Identifiers (URIs).

Web services developed using the REST style are termed RESTful

web services.

30

Page 16: CMP 436/774 Introduction to Webservices

16

Testing JAX-RS HelloWorld (1)

See HelloWorld RESTful Web Service example (main reference R1-ch 19, pp.400)

31

Testing JAX-RS HelloWorld (2)

32

Page 17: CMP 436/774 Introduction to Webservices

17

Testing JAX-RS HelloWorld (3)

33

34

Page 18: CMP 436/774 Introduction to Webservices

18

Set Project Run Property

35

JAX-RS Web Service from Database

Company database JAX-RS (left as a homework exercise)

With Google Map WS API (Software as a Service: SaaS)

Additional reference

http://netbeans.org/kb/docs/websvc/rest.html

https://developers.google.com/maps/documentation/javascript/tutorial#api_key

36

Page 19: CMP 436/774 Introduction to Webservices

19

37

38