Java Web services

646 views
595 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
646
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java Web services

  1. 1. Java EE 6 – Web Service Vladan Pulec
  2. 2. Objectives  Overview  Distinguish between the two types of web services  RESTful Service & Demo  Web Service & Demo
  3. 3. Key Features • Platform independent • Language independent • Interoperable across disparate programming languages • Leveraging existing technologies (HTTP, XML) • Supported in SE and EE version of Java
  4. 4. Benefits • Flexibility of supporting unknown future client platforms • Use of HTTP • Easier communication via firewalls and proxies • Flexible to use a variety of transmission protocols (ie. SMTP)
  5. 5. Disadvantages • Verbose (can be slower than other middleware technologies) • Relying on HTTP, the roles are fixed (only one party can use the service of the other). – Service cannot push, client must pull
  6. 6. Web Service Standards • There are two prevailing types: • Representational State Transfer (REST) – JSR 311 • Simple Object Access Protocol (SOAP) – JSR 224
  7. 7. RESTful Web Services • Representation State Transfer (REST) revolve around resources (candidate, client, etc) • The state of the resource is captured and transferred using the service
  8. 8. JAX-RS API • Does not require any specific data format • Often CSV, JSON (JavaScript Object Notation), or XML • Provides only server-side API • Can be combined with JAXB or any other Java XML API
  9. 9. Web Service Endpoints • Remotely executable components that exist on the server and are executed as a result of receiving a web service request. • Both JAX-RS and JAX-WS can use the following: • Annotated POJOs • Session bean components (Stateless or Singleton beans only)
  10. 10. JAVA-RS Endpoints • The end point is: • Annotated class created to provide web service functionality • Is instantiated per request • Does not require an EJB container • JAVA-RS will provide a servlet implementation to handle the requests
  11. 11. JAVA-RS Web Endpoints • Must have the following: – @javax.ws.rs.Path class annotation – Public methods that are annotated with method designator (ie. @GET) – Uses @Produces and/or @Consumers annotations – Cannot be abstract • Web.xml must have a web a JAVA-RS servlet configured – Classes with @Path annotation will be handled by it.
  12. 12. REST Example 1. A resource is given a specific URL (such as http://localhost/clients/adobe) 2. HTTP methods are used to perform operations – HTTP GET – retrieves the resource representation – HTTP POST – adds new element to the resource – HTTP PUT – creates or updates resource – HTTP DELETE – deletes the resource • Content can be of any MIME Type (text, XML, etc.)
  13. 13. SOAP Web Services • Simple Object Access Protocol (SOAP) • More complex than REST but provides more benefits • To implement SOAP client, developer only needs to know the Web Services Description Language (WSDL) file, which exposes all API information • Utilizes HTTP request/response and XML
  14. 14. JAX-WS • Replaces JAX-RPC • Required minimal knowledge of XML or WSDL to use basic services • Provides both server and client APIssd SOAP Client Service JAX-WS runtime JAX-WS runtimeSOAP Message
  15. 15. Sample SOAP Request • <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/“ xmlns:xsd="http://www.w3.org/2001/XMLSchema“ xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance"> • <SOAP-ENV:Header> • <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext- 1.0.xsd"> • <wsse:UsernameToken> • <wsse:Username>123456</wsse:Username> • <wsse:Password>mypassword</wsse:Password> • </wsse:UsernameToken> • <service_attributes ignore_warnings="false" /> • </wsse:Security> • </SOAP-ENV:Header> • <SOAP-ENV:Body> • <ser:findByTestCenterID xmlns:ser="http://services.capacity.vue/"> • <testCenterID>4</testCenterID> • </ser:findByTestCenterID> • </SOAP-ENV:Body> • </SOAP-ENV:Envelope> sd SOAP Envelope Header Body
  16. 16. Sample SOAP Response • <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> • <soap:Body> • <ns2:findByTestCenterIDResponse xmlns:ns2="http://services.capacity.vue/"> • <workstationSearchResponse> • <testCenter id="4" name="Electronic Systems" tws="-1" /> • <workstationRules> • <workstationRule activeRuleCount="1"> • <asset assetCategoryCode="Workstation" assetID="53412" • assetName="Test 7" siteID="4" isEnabled="true" /> • </workstationRule> • </workstationRules> • </workstationSearchResponse> • </ns2:findByTestCenterIDResponse> • </soap:Body> • </soap:Envelope>
  17. 17. JAVA-WS Endpoints • Must have the following: – @javax.jws.WebService annotation – Public methods (cannot be final or static) with @javax.jws.WebMethod annotation – Class cannot be abstract or final – Must have no args constructor
  18. 18. Data Types • JAX-WS does not contain JAVA to XML binding • Java Architecture for XML Binding (JAXB) is designed to handle the binding • Basic Java types are supported, complex ones require JAXB programming
  19. 19. Java APIs for Web Services JDOM – provides OO Java model of an XML document JAXP – abstraction of an XML processing JAXB – converts objects to XML schemas JAX-RPC – remote access API (replaced by JAX-WS) JAXR – standard for using UDDI registries SAAJ – standard for transmitting and parsing SOAP messages JAX-RS – RESTful API JAX-WS – high-level web service API
  20. 20. Additional Resources JSR 224 – Java API for Web Services (JAX-WS) JSR 331 – Java API for Restful Services (JAX-RS)

×