117E. Developing RESTful Services with Spring
Version 3.1

Book cover

This course enables the experienced Java developer to use the Spring application framework to manage objects in a lightweight "IoC" (inversion-of-control) container; to create simple and complex RESTful web services; to manage persistent objects using Spring's support for DAOs and transaction control; and to take advantage of the Spring testing framework for their RESTful services. Spring is a far-reaching framework that aims to facilitate all sorts of Java development, including every level of multi-tier distributed systems. Here we focus on the Core and Web/MVC modules, with a slightly lighter touch on persistence through DAO and ORM modules, all to the end of building RESTful web services. We also cover Spring's support for building REST clients.


Learning Objectives

  • Understand the scope, purpose, and architecture of Spring
  • Use Spring's bean factories and application contexts to declare application components, rather than hard-coding their states and lifecycles
  • Use dependency injection to further control object relationships from outside the Java code base
  • Use annotations to take advantage of Spring post-processors for automated bean instantiation and wiring
  • Build web applications and RESTful services as a Spring DispatcherServlet and associated application context
  • Use Spring MVC annotations to develop map request URLs, methods, content types, and parameters to Java methods, and to bind request data to method parameters
  • Validate input via HTTP requests, and use exception handlers to produce appropriate HTTP error responses
  • Build REST clients using Spring's "REST template"
  • Connect REST controllers to persistent stores using Spring's DAO and ORM modules
  • Integrate JPA entities and DAOs into Spring applications
  • Declare and enforce transaction requirements using Spring
  • Use the Spring testing framework for tests of core components, REST controllers, and persistence components

Timeline: 5 days.

IDE Support: Eclipse Kepler

  • In addition to the primary lab files, an optional overlay is available that adds support for Eclipse Kepler. Students can code, deploy, test, and debug all exercises from within the IDE. See also our orientation to Using Capstone's Eclipse Overlays.

Chapter 1. Overview of Spring

  • Java EE: The Good, The Bad, and the Ugly
  • Enter the Framework
  • Spring Value Proposition
  • The Spring Container
  • Web Applications
  • Persistence Support
  • Aspect-Oriented Programming
  • The Java EE Module(s)
  • Integrating Other Frameworks

Chapter 2. The Container

  • JavaBeans, Reconsidered
  • The Factory Pattern
  • Inversion of Control
  • XML View: Declaring Beans
  • Java View: Using Beans
  • Singletons and Prototypes

Chapter 3. Instantiation and Configuration

  • Configuring Through Properties
  • Configuration Namespaces
  • The p: Notation
  • Bean (Configuration) Inheritance
  • Configuring Through Constructors
  • Bean Post-Processors
  • Lifecycle Hooks
  • Integrating Existing Factory Code
  • Awareness Interfaces

Chapter 4. Dependency Injection

  • Complex Systems
  • Assembling Object Graphs
  • Dependency Injection
  • Single and Multiple Relationships
  • The Utility Schema
  • Bean Aliases
  • Inner Beans
  • Autowiring
  • @Component, @Service, & Company
  • @Autowired Properties
  • Best Practices with Spring Annotations

Chapter 5. Assembling Object Models

  • Collections and Maps
  • Support for Generics
  • The Spring Utility Schema (util:)
  • Autowiring to Multiple Beans
  • Order of Instantiation
  • Bean Factory vs. Application Context

Chapter 6. REST Basics

  • The REST Vision
  • Use of HTTP
  • Use of URIs
  • Use of Content Types
  • CRUD Operations and Business Operations
  • HATEOAS and the Richardson Maturity Model

Chapter 7. The Web Module

  • Servlets and JSPs: What's Missing
  • The MVC Pattern
  • The Front Controller Pattern
  • DispatcherServlet
  • A Request/Response Cycle
  • The Strategy Pattern
  • Web Application Contexts
  • Annotation-Based Handler Mappings
  • @Controller and @RequestMapping
  • "Creating" a Model
  • Entities, Not Views

Chapter 8. Handling Requests

  • Matching URLs
  • Matching Methods
  • Matching Content Types
  • Path Variables
  • Request Parameters
  • Headers and Cookies
  • Injectable Method Parameters
  • Command Objects vs. Entities
  • @RequestBody and @ResponseBody
  • HttpEntity<T> and ResponseEntity<T>

Chapter 9. Producing Responses

  • Return Types
  • Default Content Types
  • Default Status Codes
  • The produces Element
  • Binary Content

Chapter 10. Entities and Complex Content

  • Converters and Formatters
  • HttpMessageConverter
  • Using <mvc:annotation-driven />
  • Built-In HttpMessageConverters
  • Working with XML
  • Working with JSON
  • Custom Message Converters

Chapter 11. Validation

  • Validation in Spring MVC
  • JSR 303, a/k/a Bean Validation
  • Configuration
  • Support for @Valid
  • HandlerExceptionResolver
  • @ExceptionHandler
  • Message Sources and Localization
  • Resolving Error Codes

Chapter 12. REST Clients

  • RestTemplate
  • Sending HTTP Requests
  • Translating Entities
  • Reading Responses
  • Error Handlers

Chapter 13. Working with JPA

  • Object/Relational Mapping
  • The Java Persistence API
  • Hibernate
  • @Entity and Other JPA Annotations
  • Entity Managers and Factories
  • The Persistence Unit
  • JpaDaoSupport and JpaTemplate
  • Configuration Issues

Chapter 14. Transactions

  • Transaction Managers
  • Transaction Advice
  • AOP vs. Annotations
  • JDBC Transaction Manager
  • JPA Transaction Manager
  • Entity States
  • Extended Persistence Contexts

Chapter 15. The Spring Testing Framework

  • Testability of Spring Applications
  • Dependency Injection Of, By, and For Tests
  • Mocking
  • The Spring Test Runner
  • The Test Context
  • @ContextConfiguration
  • Profiles
  • Mocking Spring MVC
  • Testing Persistence Components

System Requirements

Hardware Requirements (Minimum) Core 2 Duo, 1.5 GHz, 4 gig RAM, 1 gig disk space.
Hardware Requirements (Recommended) Core 2 Duo, 2.5 GHz, 6 gig RAM, 1 gig disk space.
Operating System Tested on Windows 7. Course software should be viable on all systems which support a Java SE 7 Developer's Kit.
Network and Security Limited privileges required -- please see our standard security requirements.
Software Requirements All free downloadable tools.