PoshJosh's Blog

Versioning REST Resources with Spring Data REST

August 05, 2019

What is REST

REST: Representational State Transfer. REST is an architectural style, or design pattern.

The aim of REST is stateless interaction between the service provider and consumer.

In practice, given the URL of a RESTful service, you would be able to interact with that service to create, read, update or delete resources based on REST standards. You would not need to know any other information apart from those provided by the RESTful API.

For example, A RESTful web application exposes information about its resources and also enables the client to take actions on those resources, such as create new resources or change existing resources.

If well implemented. REST is intended to provide interoperability between computer systems on the internet.

Representational state transfer (REST) is a software architectural style that

defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, called RESTful Web services, provide interoperability between computer systems on the Internet. RESTful Web services allow the requesting systems to access and manipulate textual representations of Web resources by using a uniform and predefined set of stateless operations. Other kinds of Web services, such as SOAP Web services, expose their own arbitrary sets of operations.

What is Spring Data REST

Spring Data REST is part of the umbrella Spring Data project and makes it easy

to build hypermedia-driven REST web services on top of Spring Data repositories.

Spring Data REST builds on top of Spring Data repositories, analyzes your

application’s domain model and exposes hypermedia-driven HTTP resources for aggregates contained in the model.

Features

  • Exposes a discoverable REST API for your domain model using HAL as media type.
  • Exposes collection, item and association resources representing your model.
  • Supports pagination via navigational links.
  • Allows to dynamically filter collection resources.
  • Exposes dedicated search resources for query methods defined in your repositories.
  • Allows to hook into the handling of REST requests by handling Spring ApplicationEvents.
  • Exposes metadata about the model discovered as ALPS and JSON Schema.
  • Allows to define client specific representations through projections.
  • Ships a customized variant of the HAL Browser to leverage the exposed metadata.
  • Currently supports JPA, MongoDB, Neo4j, Solr, Cassandra, Gemfire.
  • Allows advanced customizations of the default resources exposed.

Versioning REST Resources with Spring Data REST

To support versioning of resources, define a version attribute for your domain objects that need this type of protection. For example:

@Entity
public class BlogPost {

	private @Id @GeneratedValue Long id;
	private String title;
	private String content;
	private Date dateCreated;

	private @Version @JsonIgnore Long version;

	private BlogPost() {}

	public BlogPost(id) {
		this.id = id;
	}
}  
  • The version field is annotated with javax.persistence.Version. It causes a

value to be automatically stored and updated every time a row is inserted and updated.

  • When fetching an individual resource (not a collection resource), Spring Data

REST automatically adds an ETag response header with the value of this field.

  • A PUT with an If-Match request header causes Spring Data REST to check the

value against the current version. If the incoming If-Match value does not match the data store’s version value, Spring Data REST will fail with an HTTP 412 Precondition Failed.

  • When someone else updates the data after you have loaded it. The update will

lead to a new version, causing the version to be different from that you loaded. When you try to update the same data, the If-Match condition will fail.

References

World Wide Web Consortium. 11 February 2004. 3.1.3 Relationship to the World Wide Web and REST Architectures. Retrieved 29 September 2016.


Written byChinomso IkwuagwuExcélsior

Limited conversations with distributed systems.

Modifying legacy applications using domain driven design (DDD)

Gherkin Best Practices

Code Review Best Practices

Hacking Cypress in 9 minutes

Some common mistakes when developing java web applications

How to make a Spring Boot application production ready

SQL JOINS - A Refresher

Add Elasticsearch to Spring Boot Application

Add entities/tables to an existing Jhipster based project

CSS 3 Media Queries - All over again

Maven Dependency Convergence - quick reference

Amazon SNS Quick Reference

AWS API Gateway Quick Reference

Amazon SQS Quick Reference

AWS API Gateway Quick Reference

AWS Lambda Quick Reference

Amazon DynamoDB - Quick Reference

Amazon Aurora

Amazon Relational Database Service

AWS Database Services

AWS Security Essentials

Amazon Virtual Private Cloud Connectivity Options

Summary of AWS Services

AWS Certified Solutions Architect - Quick Reference

AWS CloudFront FAQs - Curated

AWS VPC FAQs - Curated

AWS EC2 FAQs - Curated

AWS Achritect 5 - Architecting for Cost Optimization

AWS Achritect 4 - Architecting for Performance Efficiency

AWS Achritect - 6 - Passing the Certification Exam

AWS Achitect 3 - Architecting for Operational Excellence

AWS Achitect 2 - Architecting for Security

AWS Achitect 1 - Architecting for Reliability

Amazon DynamoDB Accelerator (DAX)

Questions and Answers - AWS Certified Cloud Architect Associate

Questions and Answers - AWS Certified Cloud Architect Associate

AWS Connectivity - PrivateLink, VPC-Peering, Transit-gateway and Direct-connect

AWS - VPC peering vs PrivateLink

Designing Low Latency Systems

AWS EFS vs FSx

AWS Regions, Availability Zones and Local Zones

AWS VPC Endpoints and VPC Endpoint Services (AWS Private Link)

AWS - IP Addresses

AWS Elastic Network Interfaces

AWS Titbits

Jenkins on AWS - Automation

Jenkins on AWS - Setup

Jenkins on AWS - Best practices

Introduction to CIDR Blocks

AWS Lamda - Limitations and Use Cases

AWS Certified Solutions Architect Associate - Part 10 - Services and design scenarios

AWS Certified Solutions Architect Associate - Part 9 - Databases

AWS Certified Solutions Architect Associate - Part - 8 Application deployment

AWS Certified Solutions Architect Associate - Part 7 - Autoscaling and virtual network services

AWS Certified Solutions Architect Associate - Part 6 - Identity and access management

AWS Certified Solutions Architect Associate - Part 5 - Compute services design

AWS Certified Solutions Architect Associate - Part 4 - Virtual Private Cloud

AWS Certified Solutions Architect Associate - Part 3 - Storage services

AWS Certified Solutions Architect Associate - Part 2 - Introduction to Security

AWS Certified Solutions Architect Associate - Part 1 - Key services relating to the Exam

AWS Certifications - Part 1 - Certified solutions architect associate

AWS Virtual Private Cloud (VPC) Examples

Curated info on AWS Virtual Private Cloud (VPC)

Notes on Amazon Web Services 8 - Command Line Interface (CLI)

Notes on Amazon Web Services 7 - Elastic Beanstalk

Notes on Amazon Web Services 6 - Developer, Media, Migration, Productivity, IoT and Gaming

Notes on Amazon Web Services 5 - Security, Identity and Compliance

Notes on Amazon Web Services 4 - Analytics and Machine Learning

Notes on Amazon Web Services 3 - Managment Tools, App Integration and Customer Engagement

Notes on Amazon Web Services 2 - Storages databases compute and content delivery

Notes on Amazon Web Services 1 - Introduction

AWS Auto Scaling - All you need to know

AWS Load Balancers - How they work and differences between them

AWS EC2 Instance Types - Curated

Amazon Web Services - Identity and Access Management Primer

Amazon Web Services - Create IAM User

Preparing Jenkins after Installation

Jenkins titbits, and then some

Docker Titbits

How to Add Chat Functionality to a Maven Java Web App

Packer - an introduction

Terraform - an introduction

Versioning REST Resources with Spring Data REST

Installing and running Jenkins in Docker

Automate deployment of Jenkins to AWS - Part 2 - Full automation - Single EC2 instance

Automate deployment of Jenkins to AWS - Part 1 - Semi automation - Single EC2 instance

Introduction to Jenkins

Software Engineers Reference - Dictionary, Encyclopedia or Wiki - For Software Engineers