You are previewing RESTful Java Web Services.
O'Reilly logo
RESTful Java Web Services

Book Description

Master core REST concepts and create RESTful web services in Java

  • Build powerful and flexible RESTful web services in Java using the most popular Java RESTful frameworks to date (Restlet, JAX-RS based frameworks Jersey and RESTEasy, and Struts 2)

  • Master the concepts to help you design and implement RESTful web services

  • Plenty of screenshots and clear explanations to facilitate learning

  • A developer's guide with practical examples to ensure proper understanding of all concepts and the differences between the frameworks studied

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.

Table of Contents

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