RESTful Java Web Services
eBook - ePub

RESTful Java Web Services

  1. 256 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

RESTful Java Web Services

About this book

In Detail

If you are already familiar with REST theory but are new to RESTful Java web services, and want to use the Java technology stack together with Java RESTful frameworks to create robust web services, this is the book for you.

This book is a guide to developing RESTful web services using Java and the most popular RESTful frameworks available today. It covers the theory of REST; practical coding examples for RESTful clients; a practical outline of the RESTful design process-yes, it's different from traditional web application development; and a complete implementation of a non-trivial web service using the following frameworks: Jersey's JAX-RS, Restlet's Lightweight REST, JBoss's JAX-RS RESTEasy, and Struts 2 with the REST plug-in.

We begin with an introduction to REST theory, for example, what it means for a web service to be RESTful, what it means for a web service to exchange representations of resources, and what it means to only use the conventional HTTP request method types-GET, POST, PUT, DELETE-for client/server communication. Before moving into the design and implementation process of a non-trivial web service, we look at a set of sample Java applications that connect to existing REST services that are freely available (Google, Yahoo, Twitter). We then outline a practical design process for RESTful web services and then cover the four most widely used frameworks available to date: JAX-RS implementations such as Jersey and RESTEasy, the Restlet framework, and Struts 2 with the REST plug-in. We cover each framework in detail to allow you to compare the strengths and weaknesses of each framework and to begin developing your own web services after the first reading; what's more, the whole source is included for you to modify and deploy in your own web services. Finally, we discuss the most common performance issues faced by RESTful web services and cover practical solutions to security-related issues such as authentication and data transfer.

A JAVA web developer's guide for building powerful and flexible RESTful web services

Approach

The approach we take is ideal for software developers with some, or extensive, programming experience: we design a RESTful API, which serves as our software specification, and implement it with every framework discussed in the book-there are no hypothetical examples; only practical working applications.

Who this book is for

This book is for Java developers who want to code RESTful web services using any of the open source RESTful frameworks available to date, for example, JAX-RS implementations such as Jersey and RESTEasy, the Restlet lightweight framework, or Struts 2 with the REST plug-in.

You don't need to know REST, as we cover the theory of REST and web services; however, you should be familiar with the Java language and have some understanding of Java web applications.

For each framework, we develop the same web service outlined in Chapter 4, so there is lots of working code available. This is a practical guide and the majority of the book is about coding RESTful web services, and not just about the theory of REST.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

RESTful Java Web Services

Jose Sandoval


Table of Contents

RESTful Java Web Services
Credits
About the Author
About the Reviewers
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Errata
Piracy
Questions
1. RESTful Architectures
What is REST?
Resources
Representation
URI
Uniform interfaces through HTTP requests
GET/RETRIEVE
POST/CREATE
PUT/UPDATE
DELETE/DELETE
Web services and the big picture
Summary
2. Accessing RESTful Services — Part 1
Getting the tools
RESTful clients
Java command-line application
Jakarta Commons HTTP Client
Java desktop application
JSP application
Servlet application
Summary
3. Accessing RESTful Services — Part 2
Getting the tools
Semantic search mashup
Application architecture
Web application definition
User interface layer
Parsing JSON structures
Servlet layer
SemanticHacker parser Servlet
Google search Servlet
Twitter search Servlet
Yahoo search Servlet
Yahoo image search Servlet
Compiling and running the application
Summary
4. RESTful Web Services Design
Designing a RESTful web service
Requirements of sample web service
Resource identification
Representation definition
XML representations
Users
Messages
JSON representations
Users
Messages
URI definition
Executing logic with RESTful URIs
Using URIs to request representation types
Summary
5. Jersey: JAX-RS
Getting the tools
JAX-RS
Jersey the JAX-RS 1.1 reference implementation
Annotations
Jersey resource
URIs
@Path
HTTP methods
@GET
@POST
@PUT
@DELETE
Relative paths in methods
URI variables
@PathParam
Input and output formats
@Consumes
@Produces
Parameters
@FormParam
Web service architecture
Persistence layer
RESTful web service implementation with Jersey
Application deployment
URI and resources
/users
HTTP GET
XML representation
JSON representation
HTTP POST
/users/{username}
HTTP GET
XML representation
JSON representation
HTTP PUT
HTTP DELETE
/messages
HTTP GET
XML representation
JSON representation
HTTP POST
/messages/{messageID}
HTTP GET
XML representation
JSON representation
HTTP DELETE
/messages/users/{username}
HTTP GET
XML representation
JSON representation
/messages/search/{search_item}
HTTP GET
XML representation
JSON representation
Using this RESTful web service
Summary
6. The Restlet Framework
Getting the tools
Restlet
Restlet 1.1
Restlet application and URI mappings
Handling HTTP requests
HTTP GET and content negotiation (HTTP Accept header)
Content negotiation and the HTTP Accept header
HTTP POST
HTTP PUT
HTTP DELETE
Implementation using Restlet 1.1
Restlet application and URI mappings
URIs and resources
/users
HTTP GET
HTTP POST
/users/{username}
HTTP GET
HTTP PUT
HTTP DELETE
/messages
/messages/{messageID}
/messages/users/{username}
/messages/search/{search_item}
Restlet 2.0
Restlet application and URI mappings
Annotations
@Get and content negotiation (HTTP Accept header)
@Post
@Put
@Delete
Implementation using Restlet 2.0
Restlet application and URI mappings
URIs and resources
/users
HTTP GET
XML representation
JSON representation
HTTP POST
/users/{username}
HTTP GET
XML representation
JSON representation
HTTP PUT
HTTP DELETE
/messages
/messages/{messageID}
/messages/users/{username}
HTTP GET
/messages/search/{search_item}
HTTP GET
Summary
7. RESTEasy: JAX-RS
Getting the tools
RESTEasy — a JAX-RS implementation
Web service architecture
RESTful web service implementation with RESTEasy
Application deployment
URI and resources
/users
/users/{username}
/messages
/messages/{messageID}
/messages/users/{username}
/messages/search/{search_item}
Summary
8. Struts 2 and the REST Plugin
Getting the tools
Struts 2
REST plugin
URI mappings
HTTP request handlers
Web service architecture
RESTful web service implementation with Struts 2
Application deployment
URIs and resources
/users and /users/{username}
HTTP GET /users
HTTP GET /users/{username}
HTTP POST /users
HTTP PUT /users/{username}
HTTP DELETE /users/{username}
/messages and /messages/{messageID}
/usermessages/{username}
/searchmessages/{search_item}
Summary
9. Restlet Clients and Servers
Getting the tools
Restlet standalone applications
Restlet clients
HTTP GET requests
HTTP POST requests
HTTP PUT requests
HTTP DELETE requests
Restlet servers
Summary
10. Security and Performance
Security
Securing web services
Custom token authentication
HTTP basic authentication
Clients and basic authentication
Servers and basic authentication
Single username and password
Tomcat realms
OAuth — accessing web services on behalf of users
Performance
High availability
Scalability
On-demand infrastructures
Performance recommendations
Summary
Index

RESTful Java Web Services

Copyright © 2009 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: November 2009
Production Reference: 1051109
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK
ISBN 978-1-847196-46-0
www.packtpub.com
Cover Image by Duraid Fatouhi ()

Credits

Author
Jose Sandoval
Reviewers
Atanas Roussev
Richard Wallace
Acquisition Editor
Sarah Cullington
Development Editor
Dhiraj Chandiramani
Technical Editor
Ishita Dhabalia
Copy Editor
Sanchari Mukherjee
Indexer
Rekha Nair
Editorial Team Leader
Gagandeep Singh
Project Team Leader
Lata Basantani
Project Coordinator
Srimoyee Ghoshal
Proofreader
Lynda Silwoski
Graphics
Nilesh R. Mohite
Production Coordinator
Dolly Dasilva
Cover Work
Dolly Dasilva

About the Author

Jose Sandoval is a software developer based in Canada. He has played and worked with web technologies since the Mosaic web browser days. For the last 12 years he's worked or consulted for various financial institutions and software companies in North America, concentrating on large-scale Java web applications. He holds a Bachelor...

Table of contents

  1. RESTful Java Web Services

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access RESTful Java Web Services by Jose Sandoval in PDF and/or ePUB format, as well as other popular books in Computer Science & Information Technology. We have over one million books available in our catalogue for you to explore.