Java web services using JAX-WS

18,804 views

Published on

JAX-WS is the replacement and next generation to JAX-RPC and makes web services development much easier using annotations and much less configuration. JAX-WS is useful for people building webservices/SOA based infrastructure as JAX-WS makes the web service development much easier and is a big gain for developer productivity.

The session uses a web service for temperature conversion example to build both the client side and Server side artifacts. Also on the server side both Servlet based and EJB3.0 based web service development will be demonstrated. JAXB concepts will be used to demonstrate the examples.

The session uses Eclipse Ganymede and Jboss 5.0. However JAX-WS being the standard, the code will smoothly work on any JavaEE based compliant servers.

Published in: Technology
0 Comments
11 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
18,804
On SlideShare
0
From Embeds
0
Number of Embeds
258
Actions
Shares
0
Downloads
556
Comments
0
Likes
11
Embeds 0
No embeds

No notes for slide

Java web services using JAX-WS

  1. 1. Java web services using JAX- WS Speaker Name Lalit Mohan Chandra Bhatt Company Name Crayom Engineering Services
  2. 2. Scope • Understanding how JAX-WS can be used to implement SOAP based web services both at server and client side.
  3. 3. Are webservices hard ! 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
  4. 4. Jargons Jargons XML XSD XSLT Xpath JAXP SAX DOM JAXB StaX SOAP WSDL UDDI JAX-RPC JAX-WS JAX-RS SAAJ WS* BP Axis Metro ESB SOA
  5. 5. Webservice Invoked initially Web service details – WSDL on XML Request - SOAP on XML Server Client Reposnse – SOAP on XML Invoked whenever message exchange happens
  6. 6. Webservice – JAX-WS way Plain old Java Object (POJO) can be easily exposed as ● web service. ● Annotation driven ● Data binding through JAXB ● Server Independent
  7. 7. JAX-WS Demo
  8. 8. JAX-WS – Servlet Way ● Write the class ● Annotate ● Register in web.xml ● Deploy – The server runtime will ➢ Generate and publish WSDL. ➢ Map SOAP request to a Java method invocation. ➢ Translate method return into a SOAP response.
  9. 9. JAX-WS – Servlet Way @WebService public class TemperatureConverter { @WebMethod public double celsiusToFarenheit(double temp){ ... } @WebMethod public double farenheitToCelsius(double temp){ ... } }
  10. 10. JAX-WS – Servlet Way <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>
  11. 11. JAX-WS Servlet Way 1. Get WSDL WSDL Published On Server 2. SOAP request 3 4 5 6 Handler JAXB Mapping Chain Java endpoint Web Service Dispatcher Clientt Endpoint Listener SOAP 11. SOAP resp 10 9 Fault 8 7 Processing
  12. 12. JAX-WS EJB 3.0 way Annotate ● Deploy ejb jar ●
  13. 13. JAX-WS – EJB 3.0 way @Stateless @WebService public class TemperatureConverter { //Rest code remains same.
  14. 14. JAX-WS – JavaSE Endpoint ● Starting Java 6 ● Generate the artifact using wsgen tool. ● wsgen tool generates JAXB mapped classes ● Use embedded HttpServer to deploy the webservice
  15. 15. JAX-WS – JavaSE Endpoint public static void main(String[] args) { TemperatureConverter tc= new TemperatureConverter(); //Java comes with an embedded Http server //which is used to host the service Endpoint endpoint = Endpoint.publish ("http://localhost:8080/tempConv", tc); //Keeping commented, keeps the server running /*endpoint.stop();*/ } }
  16. 16. JAX-WS – Client side ● Generate artifact using wsimport pointing to WSDL ● wsimport generates JAXB binding classes and service endpoint ● Call the web service using service end point
  17. 17. JAX-WS – Client side //Make the instance of service class TemperatureConverterService service = new TemperatureConverterService(); //Get port to invoke webservice TemperatureConverter port = service.getPort (TemperatureConverter.class); //Call the web service. double fahr = port.celsiusToFarenheit(100);
  18. 18. JAX-WS - start from WSDL ● JAX-WS supports start from WSDL approach @WebService(name = "TemperatureConvertor", endpointInterface="com.crayom.ws.TemperatureConvertor", targetNamespace = "http://ws.crayom.com/", wsdlLocation = "WEB-INF/TemperatureConvertorDocStyle.wsdl") public class TemperatureConvertorService implements TemperatureConvertor {
  19. 19. JAX-WS - Provider 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 following ➢ Provider<Source> ➢ Provider<SOAPMessage> ➢ Provider<DataSource>.
  20. 20. JAX-WS - Provider @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; Provide WSDL } yourself } PAYLOAD gives the body of message. MESSAGE will give whole SOAP Message
  21. 21. JAX-WS - Dispatch ● 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 ●
  22. 22. JAX-WS - Dispatch 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);
  23. 23. JAX-WS - Apart from this ● 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
  24. 24. Thank you

×