You are on page 1of 26

Web Services with JAX-WS

DSG-IDistrSys: Introduction to
Distributed Systems

11.06.2018

Lehrstuhl für Praktische Informatik


Fakultät WIAI
Otto-Friedrich-Universität Bamberg
State of the (Practical Part of the) Course
Distributed
Communication with
Sockets and JMS

JAXB

Web Service
Interaction using XML

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


2
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Big Picture
…of technologies we will need

Java implementation
Web Services
using XML
JAX-WS

WSDL
Java

SOAP
JAXB

XML + XML Schema

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


3
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
WSDL Building Blocks
<definitions name="HelloService" (…)>
<definitions>: Root WSDL Element
<message name="SayHelloRequest">
<types>: What data types will be transmitted? <part name="firstName" type="xsd:string"/>
</message>
<message>: What messages will be transmitted? <message name="SayHelloResponse">
<part name="greeting" type="xsd:string"/>
<portType>: What operations (functions) will be supported? </message>
<portType name="Hello_PortType">
<binding>: How will the messages be transmitted on the wire? <operation name="sayHello">
What SOAP-specific details are there? <input message="tns:SayHelloRequest"/>
<output message="tns:SayHelloResponse"/>
<service>: Where is the service located? </operation>
</portType>
<binding name="Hello_Binding" type="tns:Hello_PortType">
<soap:binding style="rpc"/>
<definitions>: The HelloService
<operation name="sayHello">
<message>: 1) sayHelloRequest: firstName parameter <soap:operation soapAction="sayHello"/>
2) sayHelloResponse: greeting return value
<input><soap:body use="literal"/></input>
<portType>: sayHello operation that consists of a <output><soap:body use="literal"/></output>
request/response service </operation>
</binding>
<binding>: Direction to use the SOAP HTTP transport protocol
<service name="Hello_Service">
<service>: Service available at: <port binding="tns:Hello_Binding" name="Hello_Port">
http://localhost:8080/soap/servlet/rpcrouter <soap:address location="localhost:8080/soap/servlet/rpcrouter"/>
</port>
https://katalog.ub.uni-bamberg.de/query/BV037352060 </service>
</definitions>

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


4
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
WSDL Building Blocks

https://de.wikipedia.org/wiki/Web_Services_Description_Language

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


5
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
WSDL 1.1 vs WSDL 2.0

 Several name changes


 Messages were removed
…and more

 Still no decent support in


 JAX-WS 2.0
 BPEL 2.0

 Don‘t mix it up!


 https://soa.dzone.com/news/look-wsdl-20
https://en.wikipedia.org/wiki/Web_Services_Description_Language

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


6
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
SOAP
 Used for messages in Web Service interactions

<SOAP-ENV:Envelope (…)>
SOAP message
<SOAP-ENV:Body>
Envelope (required) <sayHelloRequest>
<firstName>John</firstName>
Header (optional) </sayHelloRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Body (required)

Fault (optional)
<SOAP-ENV:Envelope (…)>
<SOAP-ENV:Body>
<sayHelloResponse>
<greeting>Hello John!</greeting>
</sayHelloResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

https://katalog.ub.uni-bamberg.de/query/BV037352060

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


7
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Code-first development
 Developing a web service starting with Java code

1. Annotate Code with JAX-WS annotations


2. Generate JAX-WS portable artifacts
3. Create a WAR file to deploy

Code Annotations Classes

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


8
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
JAX-WS: Java API for XML Web Services
 Current version 2.0
 Reference Implementation JAX-WS RI
 Annotation-based
import javax.jws.WebService;
Optional
@WebService
Service Endpoint
public interface Greetings {
Interface (SEI)
public String greet(String name);
}

@WebService(endpointInterface=“Greetings“)
public class GreetingsImpl implements Greetings {
public String greet(String name) {
return name + " says: Hello Web Services!";
}
}

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


9
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Java ← JAX-WS → WSDL

@WebParam @WebResult @WebService @SOAPBinding

@WebMethod

@XmlType @XmlRootElement

@WebService
propOrder public class Greetings {
@WebMethod
public Object greet(Object name) {
return name.getName() + " says: Hello WS!";
}
}

 See JAX-WS Specification


https://jcp.org/en/jsr/detail?id=224

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


10
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
JAXB: Java Architecture for XML Binding
 Mapping between XML structure and Java classes
 Used for WSDL types, Java parameters & return types

 Annotations on
 classes
 public variables or getter of private variables
…
JAXB samples
jaxws-samples  /type-mapping  /java-first/custom
http://www.oracle.com/technetwork/articles/javase/index-140168.html

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


11
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Data Binding with JAXB
import javax.xml.bind.annotation.*;

@XmlRootElement(name = "Chancellor")
@XmlType(propOrder = { "name", "party", "start", "to" }, namespace="http://uniba.de/dsg/jaxws/")
public class Chancellor {
@XmlElement(required=true)
public String name;
public String party;
@XmlElement(name="from")
public Date start;
public Date to;
}

 @XmlRootElement at class level


 Entry point for marshalling/unmarshalling
 All public variables and getters will be implicitly treated as XMLElement
 @XmlElement at variable / getter for advanced configuration
 @XmlElement(name="otherName") overrides the default tag name behaviour
 @XmlElement(required=true) element is required

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


12
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Many ways to host a Web Service…
 Endpoint API (without WAR)

import javax.xml.ws.Endpoint;

Endpoint.publish(
"http://localhost:8080/GreetingsService",
new GreetingsImpl()
);

 An endpoint consists of a Web Service implementation


object and configuration information.
 The API hosts the WS endpoint using a lightweight http
server.

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


13
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Many ways to host a Web Service…

 Servlet Container (e.g. Tomcat)


 e.g. WAR Deployment in webapps folder
 Deployment Descriptor needed

 Application Server (e.g. Glassfish)


 e.g. WAR Deployment in autodeploy folder or asadmin deploy task
or admin GUI
 Optional Deployment Descriptor

Deployment-styles samples
jaxws-samples  /deployment-styles

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


14
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Contract-first development
 Developing a web service starting from a WSDL

1. Generate a service endpoint interface (using wsimport tool)


2. Implement the service endpoint interface
3. Create a WAR file to deploy

WS
WSDL Interface
WS Impl

e.g. wsimport

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


15
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Two use cases for the wsimport tool

 Create a web service


 Contract-first development
 see previous slide

 Invoke a web service


 create a WS client
 will be important for the assignment

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


16
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
The wsimport tool
 The wsimport tool generates JAX-WS portable artifacts
from an existing WSDL file:

Exception Operation
classes Wrappers

Service
Client
WSDL wsimport
Service
Endpoint
Interface

Async JAXB
Response
Beans Beans

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


17
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
The wsimport tool
WSDL

WS Endpoint Interface
WS Client
Interface

ObjectFactory Package Doc


Request Response
Wrapper Wrapper Types

Factory with methods to


create Request,
Response, Type Proxies

Java Client Request, Response Type Proxies Maps the package


Proxies namespace to the
WSDL namespace

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


18
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Sample wsimport
History.wsdl

implement
HistoryService History
this interface

ObjectFactory package-info
GetChancellors
GetChancellors Chancellor
Response

*.java

wsdlimport samples
jaxws-samples  /ws-client

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


19
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Dynamic WS Client
Create a WS client with less template code than on the previous slides:

URL location = new URL(wsdlLocation);


String namespace = "http://uniba.de/dsg/idistrsys/";
QName serviceName = new QName(namespace, "JaxwsImplService");
Service service = Service.create(location, serviceName);
QName portName = new QName(namespace, "JaxwsImplPort");

ServiceInterface proxy = service.getPort(portName, ServiceInterface.class);

WSDL Reference
Server location,
WSDL service
WSDL port

WS Endpoint Interface
WS Client
Interface

Types

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


20
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
SOAP UI
 Web service testing application
 Handy from simple to complex WS test tasks

Get it!
www.soapui.org

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


21
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Hands-on Lab

JAX-WS Demo Projects


https://github.com/stefan-kolb/jaxws-samples

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


22
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Introduction to Assignment 2

Lehrstuhl für Praktische Informatik


Fakultät WIAI
Otto-Friedrich-Universität Bamberg
That‘s the Task
Extend an existing Standalone Java Application, to enable
communication with a common backend system
Client Server
LocalTicket-
ManagementBackend
(in memory)

SimpleTicketStore
Ticket- Ticket- (in memory)
Management- Search-
Backend Backend

WSRemoteAccess
WSTicket-
Management-
Backend

WSSearchService = Already Implemented

= Configuration needed
SearchServer
= Actual programming tasks

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


24
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Some Remarks
 The overall task is divided into sub-tasks with different levels of difficulty
 start with the basic level
 the “advanced” level should also be solvable for all groups
 depending on the knowledge and time in your group you should also try to solve the
“more advanced level”
 Read the general assignment guidelines
 Read the task description and supplementary material carefully
 Use the Templates provided in the VC
 This script alone is not sufficient to code the assignment
 Have a look at the example files:
 https://github.com/stefan-kolb/jaxws-samples
 Search for useful literature/tutorials/tool documentation on your own!

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


25
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg
Some more information…
 “Grading” and marks for your work
Caution! This is different to DSG-AJP-B and DSG-PKS-B!
 You will not get any “points” or “marks” for the assignments
 But: You’ll receive a short feedback mail and there will be a discussion
session which will be announced in the VC.
 But: The assignments will be part of the oral exam
 The lecture is at least as important as the practical work!
 Solving the assignment task will help you to understand the important concepts
Deadline to commit your solutions:
Sunday, July, 8th, 11:59pm (=23:59)

 Still questions?
 Read the provided material and the official documentation
 Use the VC forum
 Ask me!

11.06.2018 DSG-IDistrSys © R. Lichtenthäler, S. Winzinger, S. Kolb


26
Lehrstuhl für Praktische Informatik – WIAI – Otto-Friedrich-Universität Bamberg

You might also like