SCDJWS 5. JAX-WS
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

SCDJWS 5. JAX-WS

on

  • 1,684 views

 

Statistics

Views

Total Views
1,684
Views on SlideShare
1,684
Embed Views
0

Actions

Likes
4
Downloads
206
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

SCDJWS 5. JAX-WS Presentation Transcript

  • 1.
      JAX-WS
      Francesco Ierna
  • 2.
      JAX-WS
    • 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.
      • Replace the remote procedures call programming as defined by JAX-RPC
      • 3. Both servlet endpoints and EJB endpoints can have an object implementing the WebServiceContext interface injected using the @Resource annotation
      • 4. Allows developers to write message-oriented as well as RPC-oriented web services
      • 5. With JAX-WS, the developer does not generate or parse SOAP messages
      • 6. the platform independence of the Java programming language beacuse jax-ws uses techology definined by the W3C : HTTP, SOAP and WSDL
  • 7.
      Overview
  • 8.
      Features
      • Features :
        • Better platform indipendent
        • 9. Annotations  : supports the use of annotations.
          • Sempliies development of web services
          • 10. Much easier to expose method as web services
          • 11. Webservices.xml is optional and overrides all the information in a file class java
        • Called both synchronously and asynchronously
          • Asynchronously : Future<?> getCreditScoreAsync(Customer customer, AsyncHandler<Score> handler);
        • 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. 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. Support for MTOM  : can send binary attachments such as images or files along with Web services requests
        • 14. Support for SOAP 1.2  : JAX-WS supports both SOAP 1.1 and SOAP 1.2
        • 15. Development tools  : JAX-WS provides the wsgen and wsimport command-line tools for generating portable artifacts for JAX-WS Web services
        • 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. 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
  • 18.
      Development MessageContext
    • Using a WebServiceContext object, the endpoint have access to basic security mechanisms of JavaEE
    • 19. Both servlet endpoints and EJB endpoints can have an object implementing the WebServiceContext interface injected using the @Resource annotation
  • 20.
      Development
      • Java Class :
        • @WebServices on java class
          • No @WebMethod -> all method expose to be a web services method
          • 21. @WebMethod -> only the methods annotates with @WebMethod will be exposed to be a web services method
          • 22. We can exclude some methods by annotating the method with the property exclude setted to true into the @WebMethod annotation
      • Interface
        • Is mapped in portType within the WSDL file
      • Method
        • Is mapped to a wsdl:operation element in the corrisponding wsdl:portType plus one or more wsdl:message elements
  • 23.
      Development Example
    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.
      WSDL Services
  • 25.
      JAX-WS
  • 26.
      JAX-WS
  • 27.
      JAXB Annotations
  • 28.
      Common – Web Service Feature
  • 29.
      WSDL - WebServices
    • 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
      • Name : wsdl:portType
      • 30. TargetNameSpace : XML namespace
      • 31. ServiceName : wsdl:service
      • 32. EndpointInterface : The qualified name of the service endpoint interface. This annotation allows the separation of interface contract from implementation.
      • 33. PortName : wsdl:portName
      • 34. WsdlLocation : Specifies the Web address of the WSDL document defining the Web service. relative or absolute
    • javax.jws.WebMethod  : is to expose a method as a web service operation.
      • OperationName : The name of the wsdl:operation matching this method
      • 35. Action : The XML namespace of the the WSDL and some of the XML elements generated from this web service.
      • 36. Exclude : Used to exclude a method from the WebService. Default false
    • javax.jws.OneWay  : to mark a method as a web service one-way operation
  • 37.
      WSDL – WebServices 2
    • javax.jws.WebParam  : is used to customize the mapping of a single parameter to a message part or element
      • Name : Name of the parameter. this is name of the wsdl:part
      • 38. mode: Represents the direction the parameter flows for this method. Possible values are IN , INOUT and OUT . INOUT and OUT modes
      • 39. PartName : Used to specify the partName for the parameter with RPC or DOCUMENT/BARE operations
    • javax.jws.WebResult  : is used to customize the mapping of the method return value to a WSDL part or XML element.
      • name: The name of the return value in the WSDL and on the wire
      • 40. partName: Used to specify the partName for the result with RPC or DOCUMENT/BARE operations
    • javax.jws.HandlerChain  : is used to specified an externally defined handler chain
      • File : Location of the file containing the handler chain definition. relative or absolute with in a classpath system
      • 41. Name : DEPRECATED:
  • 42.
      WSDL – WebServices 3
    • javax.jws.soap.SOAPBinding  : to specify a SOAPBinding annotation on a endpoint implementation or service endpoint interface
      • 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. 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. document/literal bare -> can only have one such parameter
        • Style : Defines the style for messages used in a web service. The value can be either DOCUMENT or RPC . Def : DOCUMENT
        • 45. Use : Defines the encoding used for messages used in web service. Can only be LITERAL for JAX-WS 2.1. Def LITERAL
        • 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
    • javax.xml.ws.BindingType  : is used to specify the binding to use for a web service endpoint implementation class
    • 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. 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
  • 49.
      JAX - WS
    • javax.xml.ws.BindingType  : is used to specify the binding to use for a web service endpoint implementation class
    • 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. 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. 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. javax.xml.ws.WebEndpoint : Used to annotate the get PortName() methods of a generated service interface
    • 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. javax.xml.ws.WebServiceClient : specified in this annotation is sufficient to uniquely identify a wsdl:service element inside a WSDL document
    • 56. javax.xml.ws.WebServiceProvider : Annotation used to annotate a Provider implementation class
  • 57.
      JAX - WS
    • 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. 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. javax.xml.ws.Action : allows explicit association of Action message addressing property with input, output, and fault messages of the mapped WSDL operation.
      • can be specified on each method of a service endpoint interface or implementation
        • Input
        • 60. Output
        • Fault can be specified on each method of a service endpoint interface or implementation
    • 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.
  • 61.
      Tools
    • Tools for converting WSDLs and Java source/class files to Web Services
      • 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. WsGen  : Tool to process a compiled javax.jws.WebService annotated class and to generate the necessary classes to make it a Web service
      • 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
  • 64.
      Create WebService using JAX-WS
    • These are the basic steps for creating the web service and client
    • 65. 1. Code the implementation class.
    • 66. 2. Compile the implementation class.
    • 67. 3. Use wsgen to generate the artifacts required to deploy the service.
    • 68. 4. Package the files into a WAR file.
    • 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. 6. Code the client class.
    • 71. 7. Use wsimport to generate and compile the web service artifacts needed to connect to the service.
    • 72. 8. Compile the client class.
    • 73. 9. Run the client.
  • 74. Requirements of a JAX-WS Endpoint
    • JAX-WS endpoints must follow these requirements
      • The implementing class must be annotated with either the javax.jws.WebService or javax.jws.WebServiceProvider annotation.
      • 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. The business methods of the implementing class must be public , and must NOT be declared static or final .
      • 77. Business methods that are exposed to web service clients must be annotated with javax.jws.WebMethod .
      • 78. Business methods that are exposed to web service clients must have JAXB-compatible parameters and return types.
      • 79. The implementing class must NOT be declared final and must NOT be abstract .
      • 80. The implementing class must have a default public constructor .
      • 81. The implementing class must NOT define the finalize() method.
      • 82. The implementing class may use the javax.annotation.PostConstruct or javax.annotation.PreDestroy annotations on its methods for life cycle event callbacks.
  • 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. Simple JAX-WS Client
    • 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
      • 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. Static : @WebServiceRef(wsdlLocation = &quot;http://localhost:8080/JAX-WS_Server_wsgen/CalculatorService?wsdl&quot;)
    • 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
    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. 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. JAX-WS Client Communications Models
    • Dispatch client : javax.xml.ws.Dispatch
      • 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. use XML messages for communications between Web services and Web services clients
      • 89. invoking services at the XML message level is provided by the Dispatch client API
      • 90. The Dispatch client can send data in either MESSAGE or PAYLOAD mode
      • 91. The Dispatch client supports the following types of objects :
        • javax.xml.transform.Source  : can use Source objects with SOAP or HTTP bindings
        • 92. JAXB objects  : JAXB objects can only be used with SOAP or HTTP bindings
        • 93. javax.xml.soap.SOAPMessage  : You can only use SOAPMessage objects with SOAP
        • 94. javax.activation.DataSource  : Use DataSource only with HTTP bindings.
      • Dispatch client is invoked in one of three ways :
        • Synchronous  : requests and responses using the invoke method
        • 95. Asynchronous  : requests and responses using the invokeAsync method with a callback or polling object
        • 96. One-way invocation using the invokeOneWay methods
    • Dynamic Proxy client
      • Use this client when you want to invoke a Web service based on a service endpoint interface
      • 97. extend the java.lang.reflect.Proxy