<ul>JAX-WS </ul><ul>Francesco Ierna </ul>
<ul>JAX-WS </ul><ul><li>JAX-WS  : is a programming model that simplifies application development through support of a stan...
Both servlet endpoints and EJB endpoints can have an object implementing the  WebServiceContext  interface injected using ...
Allows developers to write message-oriented as well as RPC-oriented web services
With JAX-WS, the developer does  not generate or parse SOAP messages
the platform independence of the Java programming language beacuse jax-ws uses techology definined by the W3C :  HTTP, SOA...
<ul>Overview </ul>
<ul>Features </ul><ul><ul><li>Features : </li><ul><li>Better platform indipendent
Annotations  : supports the use of annotations.  </li><ul><li>Sempliies  development of web services
Much easier to expose  method as web services
Webservices.xml is optional  and  overrides  all the information in a file class java </li></ul><li>Called both  synchrono...
Data Binding  : JAX-WS leverages the Java Architecture for XML Binding (JAXB) 2.1 API and tools as the binding technology ...
Support for MTOM  : can send binary attachments such as images or files along with Web services requests
Support for SOAP 1.2  : JAX-WS supports both SOAP 1.1 and SOAP 1.2
Development tools  : JAX-WS provides the  wsgen   and  wsimport   command-line tools for generating portable artifacts for...
Support for WS-Addressing (JAX-WS 2.1) :  The new API enables you to create, transmit and use endpoint references to targe...
Support for JAX-WS 2.1 features :  JAX-WS 2.1 introduces the concept of features as a way to programmatically control cert...
<ul>Development MessageContext </ul><ul><li>Using a  WebServiceContext  object,  the endpoint have access to basic securit...
Both servlet endpoints and EJB endpoints can have an object implementing the  WebServiceContext  interface injected using ...
<ul>Development </ul><ul><ul><li>Java Class : </li><ul><li>@WebServices on java class </li><ul><li>No @WebMethod -> all me...
@WebMethod -> only the methods annotates with @WebMethod will be exposed  to be a web services method
We can exclude some methods by annotating the method with the property exclude   setted to true into the @WebMethod annota...
<ul>Development Example </ul>JAX-WS  HelloWorld  SEI: @WebService(name = &quot;HelloWorld&quot;, targetNamespace = &quot;u...
<ul>WSDL Services </ul>
Upcoming SlideShare
Loading in …5
×

SCDJWS 5. JAX-WS

2,456 views

Published on

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

No Downloads
Views
Total views
2,456
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
232
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

SCDJWS 5. JAX-WS

  1. 1. <ul>JAX-WS </ul><ul>Francesco Ierna </ul>
  2. 2. <ul>JAX-WS </ul><ul><li>JAX-WS  : is a programming model that simplifies application development through support of a standard, annotation-based model to develop Web Service applications and clients. </li></ul><ul><ul><li>Replace the remote procedures call programming as defined by JAX-RPC
  3. 3. Both servlet endpoints and EJB endpoints can have an object implementing the WebServiceContext interface injected using the @Resource annotation
  4. 4. Allows developers to write message-oriented as well as RPC-oriented web services
  5. 5. With JAX-WS, the developer does not generate or parse SOAP messages
  6. 6. the platform independence of the Java programming language beacuse jax-ws uses techology definined by the W3C : HTTP, SOAP and WSDL </li></ul></ul>
  7. 7. <ul>Overview </ul>
  8. 8. <ul>Features </ul><ul><ul><li>Features : </li><ul><li>Better platform indipendent
  9. 9. Annotations  : supports the use of annotations. </li><ul><li>Sempliies development of web services
  10. 10. Much easier to expose method as web services
  11. 11. Webservices.xml is optional and overrides all the information in a file class java </li></ul><li>Called both synchronously and asynchronously </li><ul><li>Asynchronously : Future<?> getCreditScoreAsync(Customer customer, AsyncHandler<Score> handler); </li></ul><li>Using resource injection  : The application server also supports the usage of the @Resource or @WebServiceRef annotation to declare JAX-WS managed clients and to request injection of JAX-WS services and ports
  12. 12. Data Binding  : JAX-WS leverages the Java Architecture for XML Binding (JAXB) 2.1 API and tools as the binding technology for mappings between Java objects and XML documents.
  13. 13. Support for MTOM  : can send binary attachments such as images or files along with Web services requests
  14. 14. Support for SOAP 1.2  : JAX-WS supports both SOAP 1.1 and SOAP 1.2
  15. 15. Development tools  : JAX-WS provides the wsgen and wsimport command-line tools for generating portable artifacts for JAX-WS Web services
  16. 16. Support for WS-Addressing (JAX-WS 2.1) : The new API enables you to create, transmit and use endpoint references to target a specific Web service endpoint
  17. 17. Support for JAX-WS 2.1 features : JAX-WS 2.1 introduces the concept of features as a way to programmatically control certain functions or behaviors </li></ul></ul></ul>
  18. 18. <ul>Development MessageContext </ul><ul><li>Using a WebServiceContext object, the endpoint have access to basic security mechanisms of JavaEE
  19. 19. Both servlet endpoints and EJB endpoints can have an object implementing the WebServiceContext interface injected using the @Resource annotation </li></ul>
  20. 20. <ul>Development </ul><ul><ul><li>Java Class : </li><ul><li>@WebServices on java class </li><ul><li>No @WebMethod -> all method expose to be a web services method
  21. 21. @WebMethod -> only the methods annotates with @WebMethod will be exposed to be a web services method
  22. 22. We can exclude some methods by annotating the method with the property exclude setted to true into the @WebMethod annotation </li></ul></ul><li>Interface </li><ul><li>Is mapped in portType within the WSDL file </li></ul><li>Method </li><ul><li>Is mapped to a wsdl:operation element in the corrisponding wsdl:portType plus one or more wsdl:message elements </li></ul></ul></ul>
  23. 23. <ul>Development Example </ul>JAX-WS HelloWorld SEI: @WebService(name = &quot;HelloWorld&quot;, targetNamespace = &quot;urn:samples.ibm.com/HelloWorld&quot;) public interface HelloWorld { @WebMethod (action = &quot;urn:samples.ibm.com/HelloWorld/hello&quot;) @WebResult (name = &quot;response&quot;, targetNamespace = &quot;&quot;) @RequestWrapper (localName = &quot; hello &quot;, targetNamespace = &quot;urn:samples.ibm.com/HelloWorld&quot;, className = &quot;com.ibm.samples.helloworld.Hello&quot;) @ResponseWrapper (localName = &quot; helloResponse &quot;, targetNamespace = &quot;urn:samples.ibm.com/HelloWorld&quot;, className = &quot;com.ibm.samples.helloworld.HelloResponse&quot;) public String hello( @WebParam(name = &quot;name&quot;, targetNamespace = &quot;&quot;) String name);} <wsdl:types> <xsd:schema targetNamespace=&quot;urn:helloWorld/sample/ibm/com&quot;...> <xsd:element name=&quot;hello&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;helloResponse&quot; type=&quot;xsd:string&quot;/> </xsd:schema> </wsdl:types> <wsdl:message name=&quot; helloRequestMsg &quot;> <wsdl:part element=&quot;tns:hello&quot; name=&quot;helloParameters&quot; /> </wsdl:message> <wsdl:message name=&quot; helloResponseMsg &quot;> <wsdl:part element=&quot;tns:helloResponse&quot; name=&quot;helloResult&quot; /> </wsdl:message>
  24. 24. <ul>WSDL Services </ul>
  25. 25. <ul>JAX-WS </ul>
  26. 26. <ul>JAX-WS </ul>
  27. 27. <ul>JAXB Annotations </ul>
  28. 28. <ul>Common – Web Service Feature </ul>
  29. 29. <ul>WSDL - WebServices </ul><ul><li>javax.jws.WebService : to mark a endpoint implementation as implementing a web service or to mark that a service endpoint interface as defining a web service interface </li></ul><ul><ul><li>Name : wsdl:portType
  30. 30. TargetNameSpace : XML namespace
  31. 31. ServiceName : wsdl:service
  32. 32. EndpointInterface : The qualified name of the service endpoint interface. This annotation allows the separation of interface contract from implementation.
  33. 33. PortName : wsdl:portName
  34. 34. WsdlLocation : Specifies the Web address of the WSDL document defining the Web service. relative or absolute </li></ul></ul><ul><li>javax.jws.WebMethod  : is to expose a method as a web service operation. </li></ul><ul><ul><li>OperationName : The name of the wsdl:operation matching this method
  35. 35. Action : The XML namespace of the the WSDL and some of the XML elements generated from this web service.
  36. 36. Exclude : Used to exclude a method from the WebService. Default false </li></ul></ul><ul><li>javax.jws.OneWay  : to mark a method as a web service one-way operation </li></ul>
  37. 37. <ul>WSDL – WebServices 2 </ul><ul><li>javax.jws.WebParam  : is used to customize the mapping of a single parameter to a message part or element </li></ul><ul><ul><li>Name : Name of the parameter. this is name of the wsdl:part
  38. 38. mode: Represents the direction the parameter flows for this method. Possible values are IN , INOUT and OUT . INOUT and OUT modes
  39. 39. PartName : Used to specify the partName for the parameter with RPC or DOCUMENT/BARE operations </li></ul></ul><ul><li>javax.jws.WebResult  : is used to customize the mapping of the method return value to a WSDL part or XML element. </li></ul><ul><ul><li>name: The name of the return value in the WSDL and on the wire
  40. 40. partName: Used to specify the partName for the result with RPC or DOCUMENT/BARE operations </li></ul></ul><ul><li>javax.jws.HandlerChain  : is used to specified an externally defined handler chain </li></ul><ul><ul><li>File : Location of the file containing the handler chain definition. relative or absolute with in a classpath system
  41. 41. Name : DEPRECATED: </li></ul></ul>
  42. 42. <ul>WSDL – WebServices 3 </ul><ul><li>javax.jws.soap.SOAPBinding  : to specify a SOAPBinding annotation on a endpoint implementation or service endpoint interface </li></ul><ul><ul><li>lets the developer choose between document/literal wrapped, document/literal bare, rpc/literal and rpc/encoded endpoints with the default being document/literal wrapped
  43. 43. document/literal wrapped -> is that methods on a document/literal wrapped endpoint can have multiple parameters bound to the body of a SOAP message,
  44. 44. document/literal bare -> can only have one such parameter </li><ul><li>Style : Defines the style for messages used in a web service. The value can be either DOCUMENT or RPC . Def : DOCUMENT
  45. 45. Use : Defines the encoding used for messages used in web service. Can only be LITERAL for JAX-WS 2.1. Def LITERAL
  46. 46. ParameterStyle : Determines if the method's parameters represent the entire message body or whether the parameters are wrapped in a body element named after the operation </li></ul></ul></ul><ul><li>javax.xml.ws.BindingType  : is used to specify the binding to use for a web service endpoint implementation class
  47. 47. javax.xml.ws.RequestWrapper  : annotates methods in the Service Endpoint Interface with the request wrapper bean to be used at runtime. Only the className is required in this case
  48. 48. javax.xml.ws.ResponseWrapper  : annotates methods in the Service Endpoint Interface with the response wrapper bean to be used at runtime. Only the className is required in this case </li></ul>
  49. 49. <ul>JAX - WS </ul><ul><li>javax.xml.ws.BindingType  : is used to specify the binding to use for a web service endpoint implementation class
  50. 50. javax.xml.ws.RequestWrapper  : annotates methods in the Service Endpoint Interface with the request wrapper bean to be used at runtime. Only the className is required in this case
  51. 51. javax.xml.ws.ResponseWrapper  : annotates methods in the Service Endpoint Interface with the response wrapper bean to be used at runtime. Only the className is required in this case
  52. 52. javax.xml.ws.ServiceMode : allows the Provider developer to indicate whether a Provider implementation wishes to work with entire protocol messages or just with protocol message payloads
  53. 53. javax.xml.ws.WebEndpoint : Used to annotate the get PortName() methods of a generated service interface
  54. 54. javax.xml.ws.WebFault : This annotation is generated by the JAX-WS tools into service specific exception classes generated from a WSDL to customize to the local and namespace name of the fault element and the name of the fault bean and to mark the service specific exception as one generated from WSDL
  55. 55. javax.xml.ws.WebServiceClient : specified in this annotation is sufficient to uniquely identify a wsdl:service element inside a WSDL document
  56. 56. javax.xml.ws.WebServiceProvider : Annotation used to annotate a Provider implementation class </li></ul>
  57. 57. <ul>JAX - WS </ul><ul><li>javax.xml.ws.WebServiceRef : is used to define a reference to a web service and (optionally) an injection target for it. Web service references are resources in the Java EE 5 sense.
  58. 58. javax.xml.ws.Feature : The @Feature annotation is used to enable/disable a feature to use for a web service endpoint implementation class. This annotation is used with @BindingType annotation.
  59. 59. javax.xml.ws.Action : allows explicit association of Action message addressing property with input, output, and fault messages of the mapped WSDL operation. </li><ul><li>can be specified on each method of a service endpoint interface or implementation </li><ul><li>Input
  60. 60. Output </li></ul></ul></ul><ul><ul><ul><li>Fault can be specified on each method of a service endpoint interface or implementation </li></ul></ul></ul><ul><li>javax.xml.ws.FaultAction : is used inside an @Action annotation to allow an explicit association of Action message addressing property with the fault messages of the WSDL operation mapped from the exception class. </li></ul>
  61. 61. <ul>Tools </ul><ul><li>Tools for converting WSDLs and Java source/class files to Web Services </li></ul><ul><ul><li>APT  : A Java SE tool and framework for processing annotations. APT will invoke a JAX-WS AnnotationProcossor for processing Java source files with javax.jws.* annotations and making them web services
  62. 62. WsGen  : Tool to process a compiled javax.jws.WebService annotated class and to generate the necessary classes to make it a Web service
  63. 63. WsImport  : Tool to import a WSDL and to generate an SEI (a javax.jws.WebService ) interface that can be either implemented on the server to build a webservice, or can be used on the client to invoke the web service </li></ul></ul>
  64. 64. <ul>Create WebService using JAX-WS </ul><ul><li>These are the basic steps for creating the web service and client
  65. 65. 1. Code the implementation class.
  66. 66. 2. Compile the implementation class.
  67. 67. 3. Use wsgen to generate the artifacts required to deploy the service.
  68. 68. 4. Package the files into a WAR file.
  69. 69. 5. Deploy the WAR file. The web service artifacts (which are used to communicate with clients) are generated by the Application Server during deployment.
  70. 70. 6. Code the client class.
  71. 71. 7. Use wsimport to generate and compile the web service artifacts needed to connect to the service.
  72. 72. 8. Compile the client class.
  73. 73. 9. Run the client. </li></ul>
  74. 74. Requirements of a JAX-WS Endpoint <ul><li>JAX-WS endpoints must follow these requirements </li><ul><li>The implementing class must be annotated with either the javax.jws.WebService or javax.jws.WebServiceProvider annotation.
  75. 75. The implementing class may explicitly reference an SEI through the endpointInterface element of the @WebService annotation, but is not required to do so. If no endpointInterface is specified in @WebService , an SEI is implicitly defined for the implementing class.
  76. 76. The business methods of the implementing class must be public , and must NOT be declared static or final .
  77. 77. Business methods that are exposed to web service clients must be annotated with javax.jws.WebMethod .
  78. 78. Business methods that are exposed to web service clients must have JAXB-compatible parameters and return types.
  79. 79. The implementing class must NOT be declared final and must NOT be abstract .
  80. 80. The implementing class must have a default public constructor .
  81. 81. The implementing class must NOT define the finalize() method.
  82. 82. The implementing class may use the javax.annotation.PostConstruct or javax.annotation.PreDestroy annotations on its methods for life cycle event callbacks. </li></ul></ul>
  83. 83. Example package helloservice.endpoint; import javax.jws.WebService; @WebService public class Hello { private String message = new String(&quot;Hello, &quot;); public void Hello() {} @WebMethod public String sayHello(String name) { return message + name + &quot;.&quot;; } }
  84. 84. Simple JAX-WS Client <ul><li>A JAX-WS web service client can be either a dynamic client, for which no artifacts have been generated, or a static client for which a set of artifacts have been generated </li></ul><ul><ul><li>Dynamic : Dynamic clients can choose to work either with protocol-specific message structures, e.g. SOAP messages for the SOAP protocol binding, or with message payloads, which in the case of SOAP would be the contents of the SOAP body in a message
  85. 85. Static : @WebServiceRef(wsdlLocation = &quot;http://localhost:8080/JAX-WS_Server_wsgen/CalculatorService?wsdl&quot;) </li></ul></ul><ul><li>J HelloClient is a stand-alone Java program that accesses the sayHello method of HelloService . It makes this call through a port, a local object that acts as aproxy for the remote service. The port is created at development time by the wsimport tool, which generates JAX-WS portable artifacts based on a WSDL file </li></ul>1. Uses the javax.xml.ws.WebServiceRef annotation to declare a reference to a web service. @WebServiceRef (wsdlLocation=&quot;http://localhost:8080/helloservice/hello?wsdl&quot;) static HelloService service; 2. Retrieves a proxy to the service, also known as a port, by invoking getHelloPort on the service Hello port = service.getHelloPort(); 3. The port implements the SEI defined by the service. String response = port.sayHello(name);
  86. 86. Example public class HelloClient { @WebServiceRef(wsdlLocation=&quot;http://localhost:8080/helloservice/hello?wsdl&quot;) static HelloService service; public static void main(String[] args) { try { HelloClient client = new HelloClient(); client.doTest(args); } catch(Exception e) { e.printStackTrace();}} public void doTest(String[] args) { try { System.out.println(&quot;Retrieving the port from the following service: &quot; + service); Hello port = service.getHelloPort(); System.out.println(&quot;Invoking the sayHello operation on the port.&quot;); String name; if (args.length > 0) { name = args[0]; } else { name = &quot;No Name&quot;; } String response = port.sayHello(name); System.out.println(response); } catch(Exception e) { e.printStackTrace();}}}
  87. 87. JAX-WS Client Communications Models <ul><li>Dispatch client : javax.xml.ws.Dispatch </li><ul><li>Use this client when you want to work at the XML message level or when you want to work without any generated artifacts at the JAXWS level
  88. 88. use XML messages for communications between Web services and Web services clients
  89. 89. invoking services at the XML message level is provided by the Dispatch client API
  90. 90. The Dispatch client can send data in either MESSAGE or PAYLOAD mode
  91. 91. The Dispatch client supports the following types of objects : </li></ul></ul><ul><ul><ul><li>javax.xml.transform.Source  : can use Source objects with SOAP or HTTP bindings
  92. 92. JAXB objects  : JAXB objects can only be used with SOAP or HTTP bindings
  93. 93. javax.xml.soap.SOAPMessage  : You can only use SOAPMessage objects with SOAP
  94. 94. javax.activation.DataSource  : Use DataSource only with HTTP bindings. </li></ul><li>Dispatch client is invoked in one of three ways : </li><ul><li>Synchronous  : requests and responses using the invoke method
  95. 95. Asynchronous  : requests and responses using the invokeAsync method with a callback or polling object
  96. 96. One-way invocation using the invokeOneWay methods </li></ul></ul></ul><ul><li>Dynamic Proxy client </li><ul><li>Use this client when you want to invoke a Web service based on a service endpoint interface
  97. 97. extend the java.lang.reflect.Proxy </li></ul></ul>

×