WA2018 Programming REST Web Services with JAX-RS 1.1
Total Page:16
File Type:pdf, Size:1020Kb
Y L N O N IO T A U L A V E WA2018 Programming REST Web Services with JAX-RS 1.1 - WebLogic 12c / Eclipse Web Age Solutions Inc. USA: 1-877-517-6540 Canada: 1-866-206-4644 Web: http://www.webagesolutions.com The following terms are trademarks of other companies: Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. Microsoft, Windows, Windows NT, and the Windows logo are trademarks of Microsoft Corporation in the United States, other countries, or both. UNIX is a registered trademark of The Open Group in the United States and other countries. Linux is a trademark of Linus Torvalds in the United States, other countries, or both. IBM, WebSphere, DB2 and Tivoli are trademarks of the International Business Machines Corporation in the United States, other countries, or both. Other company, product, and service names may be trademarks or service marks of others. Y For customizations of this book or other sales inquiries, pleaseL contact us at: USA: 1-877-517-6540, email: [email protected] Canada: 1-866-206-4644 toll free, email: [email protected] O N Copyright © 2012 Web Age Solutions Inc. O This publication is protected by the copyright lawsI of Canada, United States and any other country where this book is sold. UnauthorizedT use of this material, including but not limited to, reproduction of the whole or partA of the content, re-sale or transmission through fax, photocopy or e-mail is prohibited.U To obtain authorization for any such activities, please write to: L Web Age Solutions Inc. A 439 University Ave V Suite 820 E Toronto Ontario, M5G 1Y8 Table of Contents Chapter 1 - REST Services..................................................................................................7 1.1 Many Flavors of Services..........................................................................................7 1.2 Understanding REST................................................................................................7 1.3 Principles of RESTful Services.................................................................................8 1.4 REST Resource Examples........................................................................................9 1.5 SOAP Equivalent Examples.....................................................................................9 1.6 REST vs SOAP Communication............................................................................10 1.7 More REST vs SOAP..............................................................................................10 1.8 REST vs SOAP Summary.......................................................................................11 1.9 Famous RESTful Services......................................................................................11 1.10 Additional Resources............................................................................................12 1.11 Summary...............................................................................................................12 Chapter 2 - Introduction to JAX-RS..................................................................................13 2.1 The JAX-RS Specification......................................................................................13 2.2 The Resource Class.................................................................................................Y 13 2.3 A Bunch of Annotations..........................................................................................L 14 2.4 @Path......................................................................................................................15 2.5 More Complex Paths...............................................................................................N 15 2.6 Using Path Parameters............................................................................................ O 16 2.7 HTTP Binding.........................................................................................................17 2.8 web.xml...................................................................................................................N 17 2.9 Summary.................................................................................................................O 18 Chapter 3 - JAX-RS Data Injection...................................................................................I 19 3.1 Sources for Injected Data........................................................................................T 19 3.2 Path Parameters.......................................................................................................A 19 3.3 Query Parameters....................................................................................................U 20 3.4 HTML Form Input..................................................................................................L 21 3.5 Cookies....................................................................................................................21 3.6 Matrix Parameters...................................................................................................A 22 3.7 HTTP Headers.........................................................................................................V 23 3.8 Default Values.........................................................................................................E 23 3.9 Parameter Conversion.............................................................................................24 3.10 Custom Types........................................................................................................24 3.11 Summary...............................................................................................................25 Chapter 4 - Designing a RESTful Service.........................................................................27 4.1 Introduction.............................................................................................................27 4.2 The Design Methodology........................................................................................27 4.3 Ingredients of a Service Operation Interface..........................................................28 4.4 What Constitutes a REST Resource........................................................................29 4.5 Resource Identifiers................................................................................................29 4.6 MIME Types...........................................................................................................30 4.7 HTTP Methods........................................................................................................30 4.8 Example Operation Interface Document.................................................................31 4.9 Summary.................................................................................................................31 Chapter 5 - JAX-RS Content Types...................................................................................33 5.1 Internet Media Types..............................................................................................33 5.2 Use of Media Type in REST...................................................................................34 5.3 The @Consumes Annotation..................................................................................34 5.4 Content Negotiation................................................................................................35 5.5 The @Produces Annotation....................................................................................36 5.6 The MediaType Class.............................................................................................36 5.7 JAXB.......................................................................................................................36 5.8 Dynamic Content Negotiation.................................................................................37 5.9 Summary.................................................................................................................38 Chapter 6 - Building Complex JAX-RS Responses..........................................................39 6.1 HTTP Response Status Codes.................................................................................39 6.2 Introduction to the JAX-RS Response Class..........................................................39 6.3 Using the Response and Response.ResponseBuilder Classes.................................40 6.4 Example: Conditional HTTP GET..........................................................................41 6.5 Returning Cookies...................................................................................................41 6.6 Cookies in Response Headers.................................................................................42 6.7 Reading Cookies.....................................................................................................Y 42 6.8 Returning an Exception...........................................................................................L 43 6.9 ExceptionMappers...................................................................................................N 44 6.10 Summary...............................................................................................................44 Chapter 7 - REST vs. SOAP.............................................................................................