Intro to Programming Java Web Services using the JavaTM API for

Download Report

Transcript Intro to Programming Java Web Services using the JavaTM API for

Intro to Programming Java Web
Services using the JavaTM API for
XMLWeb Services (JAX-WS)
Bill Champlin
UCCS / CS526
Spring ‘09
Agenda
• Project Description / Purpose
• Overview of JAX-WS and Java Web
Services
• Programming Steps
• Advantages / Disadvantages
• References
2
Project Description
• The Purpose of this Project is to Learn:
–
–
–
–
Capabilities of the JAX-WS API
Steps to Programming a Service and a Client
SOAP XML Messaging
Role of JAX-WS in Java Web Services
3
JAX-WS Overview
• Used to Build Java Web Services / Clients
• Uses SOAP Messages (also REST Msg Protocol)
– Over HTTP (also SMTP)
• Abstracts SOAP Messages from Developer
– Doesn’t Have to Write Code to Parse Messages
• NOT ALWAYS TRUE
– JAX-B Converts Java types to / from SOAP Messages
• Supports RPC or Document Messaging Styles
– RPC - Simple Types i.e. xsd:long, xsd: byte, etc
– Document - Complex Types by Creating new XML Schemas
• Supports Asynchronous Requests via Thread Pools
• Faults can be Thrown back to Clients
• Can Insert Binary Data as the Payload or as a SOAP
Attachment
4
Java Web Services
5
Basic Programming Steps [2]
1.
2.
3.
Code & Compile Annotated Java Web Service:
- Service Endpoint Interface (SEI) (Optional)
- Service Implementation Bean (SIB) Class
Run “wsgen” to Create “Artifacts”
- Java Types for Marshaling to / from XML Types
Deploy the Services using Endpoint. publish() (or High
Level Tools i.e. Metro, Glass Fish, etc.)
--> Creates the WSDL - Web Service Definition Language
Document (aka “Whiz-Dull”)
- XML file that Defines the Contract between SEI
and Clients
- WSDL can be found at: http://127.0.0.1:8888/teams?wsdl
4. Run “wsimport” to Create a Proxy SEI using WSDL
5. Code Client Against Proxy SEI
6. Execute Client
(Optional – Create Service Side or Client Side Message
Handlers)
6
Data Flow
CLIENT
H
a
n
d
l
e
r
s
H
T
T
P
H
a
n
d
l
e
r
s
SERVICE
7
JAX-WS Sample Service [1]
// Service
@WebService
public class Teams {
private TeamsUtility utils;
public Teams() {
utils = new TeamsUtility();
utils.make_test_teams();}
@WebMethod
public Team getTeam(String name) {
return utils.getTeam(name); }
@WebMethod
public List<Team> getTeams() {
return utils.getTeams(); }}
8
JAX-WS Sample Client [1]
// Client
class TeamClient {
public static void main(String[ ] args) {
TeamsService service = new TeamsService();
Teams port = service.getTeamsPort();
List<Team> teams = port.getTeams();
for (Team team : teams) {
System.out.println("Team: " + team.getName() +
" (roster count: " + team.getRosterCount() +")");
for (Player player : team.getPlayers())
System.out.println(" Player: " +
player.getNickname());
Advantages / Disadvantages
• Advantages:
–
–
–
–
Easy to Create Simple Interoperable Services
Protocol Hidden from Developer (but Available)
Java Threading allows Asynchronous Services
Faults (exceptions) can be Propagated Back to
Client
• Disadvantages:
– Rapidly Changing s/w Versions / Tools
– Complex to Modify SOAP messages using API
– Binary Attachments Require Modifying WSDL
10
References
Project Website:
http://cs.uccs.edu/~wchampli/cs526/project.html
Resources:
1.
Java Web Services: Up and Running, by Martin Kalin. Copyright 2009 Martin Kalin,
978-0-596-52112-7
2.
Sun Microsystems. The Java Web Services Tutorial for Java Web Services Developer’s
Pack, v2.0 February 17, http://java.sun.com/webservices/tutorial.html
3.
Core Java Volume 2, Advanced Features, Cay S. Horstmann and Gary Cornell, Sun
Microsystems Press, Prentice Hall, Copyright 2008, 978-0132354790, pgs 871-882.
4.
Create JAX-WS Service in 5 Minutes (Tutorial), Alexander Ananiev & MyArch,
http://myarch.com/create-jax-ws-service-in-5-minutes, Copyright © 2002-2006
5.
JAX-WS Tutorial http://www.javacoda.com/blog/?p=22
6.
stackoverflow Q&A site. http://stackoverflow.com/questions/100993/rmi-vs-webservices-whats-best-for-java2java-remoting
11