Top Banner
Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic http :// www.spiderlogic.com
30

Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Dec 18, 2015

Download

Documents

Leo Hodge
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Webservices using JAXB and JAX-WS

Lalit Bhatt SpiderLogic

http://www.spiderlogic.com

Page 2: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

JAXB JAX-WS

© www.spiderlogic.com

Agenda

Page 3: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Provides binding between XML and Java To provide a higher level of abstraction to work with. Associates a set of Java classes with XML. Think of Java class as representing the XML instances.

Reference site: https://jaxb.dev.java.net

© www.spiderlogic.com

JAXB – Java XML binding

Page 4: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

© www.spiderlogic.com

JAXB in Action – Dev time

XML schemaJAXB

Compiler

Schema derivedClasses and

interface

XML schemaJAXB

Schemagenerator

Java classes

Page 5: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

© www.spiderlogic.com

JAXB in Action – Run time

XML document JAXB API

Java objects representing XML content

unmarshel

marshel

Page 6: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Demo

© www.spiderlogic.com

JAXB

Page 7: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Advantages Ease of Use. Object oriented way of doing things.

Disadvantages Abstraction has a cost

© www.spiderlogic.com

JAXB

Page 8: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Understanding how JAX-WS can be used to implement SOAP based web services both at server and client side.

© www.spiderlogic.com

JAX-WS

Page 9: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Dave Podnar's Five Stages of Dealing with Web Services

Denial - It's Simple Object Access Protocol, right?Over Involvement - OK, I'll read the SOAP, WSDL, WS-I BP, JAX-RPC, SAAJ, JAX-P... specs. next, I'll check the Wiki and finally follow an example showing service and client sides.Anger - I can't believe those #$%&*@s made it so difficult!Guilt - Everyone is using Web Services, it must be me, I must be missing something.Acceptance - It is what it is, Web Services aren't simple or easy

© www.spiderlogic.com

Webservices

Page 10: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Web service is about SOAP, WSDL and UDDI – Web service is higher level concept than this. SOAP, WSDL and UDDI are actually implementation details.

Web services are based on RPC paradigm – Web services are document driven also.

Web services are based on HTTP – Web services can work on many other protocols like SMTP.

Web services can cure cancer

© www.spiderlogic.com

Webservices

Page 11: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

XML XSDXSLT Xpath JAXP

SAX DOM JAXB StaXSOAP WSDL UDDI RestJAX-RPC JAX-WS JAX-RS

SAAJ WS* BPAxis Metro

ESBSOA

© www.spiderlogic.com

Jargons Jargons

Page 12: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

© www.spiderlogic.com

Webservice

Client

Web service details – WSDL on XML

Request - SOAP on XML

Reposnse – SOAP on XML

Invoked initially

Invoked whenever messageexchange happens

Server

Page 13: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Plain old Java Object (POJO) can be easily exposed as web service.

Annotation driven

Data binding through JAXB

Server Independent

© www.spiderlogic.com

Webservice – JAX-WS way

Page 14: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Write the class

Annotate

Register in web.xml

Deploy – The server runtime willGenerate and publish WSDL.Map SOAP request to a Java method invocation.Translate method return into a SOAP response

© www.spiderlogic.com

JAX-WS Servlet way

Page 15: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

@WebServicepublic class TemperatureConverter {

@WebMethod public double celsiusToFarenheit(double temp){

... }

@WebMethod public double farenheitToCelsius(double

temp){ ...

}

}

© www.spiderlogic.com

JAX-WS Servlet way

Page 16: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

<servlet> <servlet-name>tempConv</servlet-name> <servlet-class> com.lalit.TemperatureConverter </servlet-class></servlet>

<servlet-mapping> <servlet-name>tempConv</servlet-name> <url-pattern>/tempConv</url-pattern></servlet-mapping>

© www.spiderlogic.com

JAX-WS Servlet way

Page 17: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

© www.spiderlogic.com

JAX-WS Servlet way

`

CLIENTt

1. Get WSDL WSDL PublishedOn Server

End

poin

tLi

stener

Disp

atch

er

HandlerChain

SOAPFault

Processing

JAX

B M

appin

g

Web S

erv

iceJa

va e

ndpoin

t

2. SOAP request 3 4 65

10 9 8 711. SOAP resp

Page 18: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Annotate

Deploy ejb jar

© www.spiderlogic.com

JAX-WS EJB3 way

Page 19: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

@Stateless@WebServicepublic class TemperatureConverter {

//Rest code remains same.

© www.spiderlogic.com

JAX-WS EJB3 way

Page 20: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Starting Java 6

Generate the artifact using wsgen tool.

wsgen tool generates JAXB mapped classes

Use embedded HttpServer to deploy the webservice

© www.spiderlogic.com

JAX-WS Java SE Endpoint

Page 21: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

public static void main(String[] args) {

TemperatureConverter tc= new

TemperatureConverter();

//Java comes with an embedded Http server//which is used to host the serviceEndpoint endpoint = Endpoint.publish

("http://localhost:8080/tempConv", tc);

//Keeping commented, keeps the server running

/*endpoint.stop();*/ }

© www.spiderlogic.com

JAX-WS Java SE Endpoint

Page 22: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Generate artifact using wsimport pointing to WSDL

wsimport generates JAXB binding classes and service endpoint

Call the web service using service end point

© www.spiderlogic.com

JAX-WS Client side

Page 23: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

//Make the instance of service classTemperatureConverterService service =

new TemperatureConverterService();

//Get port to invoke webserviceTemperatureConverter port = service.getPort (TemperatureConverter.class);

//Call the web service.double fahr = port.celsiusToFarenheit(100);

© www.spiderlogic.com

JAX-WS Client side

Page 24: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

JAX-WS supports start from WSDL approach

@WebService (name = "TemperatureConvertor", endpointInterface= "com.lalit.ws.TemperatureConvertor", targetNamespace = "http://ws.crayom.com/", wsdlLocation = "WEB-INF/TemperatureConvertorDocStyle.wsdl")public class TemperatureConvertorService implements TemperatureConvertor {

© www.spiderlogic.com

JAX-WS start From WSDL

Page 25: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Web Service endpoints may choose to work at the XML message level by implementing the Provider interface.

The endpoint accesses the message or message payload using this low-level, generic API

Implement one of the followingProvider<Source>Provider<SOAPMessage>Provider<DataSource>.

© www.spiderlogic.com

JAX-WS Provider

Page 26: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

@WebServiceProvider ( serviceName = "TempConvProvider",

portName="TempConvPort", targetNamespace = "http://www.crayom.com/om", wsdlLocation=

"WEB-INF/wsdl/TempConvProvider.wsdl")@ServiceMode(value=Service.Mode.PAYLOAD)public class TempConvProvider implements Provider<Source>{

public Source invoke(Source request) { … return resp;

}}

© www.spiderlogic.com

JAX-WS Provider

PAYLOAD gives the body of message.MESSAGE will give whole SOAP

Message

Page 27: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Web service client applications may choose to work at the XML message level by using the Dispatch<T> APIs.

The javax.xml.ws.Dispatch<T> interface provides support for the dynamic invocation of service endpoint operations.

Similar to Provider on server side

© www.spiderlogic.com

JAX-WS Dispatch

Page 28: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Service service = Service.create(url, serviceName);

Dispatch<Source> sourceDispatch = service.createDispatch(portQName,

Source.class,

Service.Mode.PAYLOAD);

//request is a XML which is put into SOAP payload StreamSource streamSource = new StreamSource

(new StringReader(request)); Source result = sourceDispatch.invoke(streamSource);

© www.spiderlogic.com

JAX-WS Dispatch

Page 29: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

JAX-WS provides support for SOAP fault handling in terms of exceptions.

JAX-WS supports handlers both on client and server side , which can process SOAP headers. SOAP headers are used to build Quality of services (QOS).

JAX-WS supports asynchronous communication

© www.spiderlogic.com

JAX-WS Apart From this

Page 30: Webservices using JAXB and JAX-WS Lalit Bhatt SpiderLogic .

Thanks

Lalit Bhatt (http://www.lalitbhatt.com)SpiderLogic (http://www.spiderlogic.com )