DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Less Code With Spring Data Rest
  • Introduction To Spring Data JPA With Inheritance in A REST Application
  • The Magic of Spring Data
  • Build Reactive REST APIs With Spring WebFlux

Trending

  • Memory-Optimized Tables: Implementation Strategies for SQL Server
  • When Airflow Tasks Get Stuck in Queued: A Real-World Debugging Story
  • Designing AI Multi-Agent Systems in Java
  • Data Lake vs. Warehouse vs. Lakehouse vs. Mart: Choosing the Right Architecture for Your Business
  1. DZone
  2. Data Engineering
  3. Data
  4. Spring Data REST and Projections

Spring Data REST and Projections

This overview of projections in Spring Data REST shows how we can use Projections in a similarly to Data Transfer Objects to control what clients see.

By 
Martin Farrell user avatar
Martin Farrell
·
Nov. 29, 17 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
41.7K Views

Join the DZone community and get the full member experience.

Join For Free

Spring Data REST and Projections is the final post in my series on using Spring Data REST. Projections allow you to control exposure to your domain objects in a similar way to Domain Transfer Objects (DTOs).

This post forms part of a series looking at Spring Data REST:

  • Introduction to Spring Data REST
  • Spring Security and Spring Data REST
  • Securing Spring Data REST with PreAuthorize
  • 4 Ways To Control Access to Spring Data REST

What Are Projections?

It is a common practice to use Domain Transfer Objects in REST API design as a method of separating the API from its underlying model. This is particularly relevant to Spring Data JPA REST where you may want to restrict what is visible to clients.

Spring Data JPA REST allows you to achieve something similar through the use of Projections.

Example

If we stick with the example used in the previous posts, we define our JPA object as:

@Entity
public class ParkrunCourse {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String courseName;
    private String url;
    private Long averageTime;
}


We then have our Spring Data JPA repository:

@RepositoryRestResource
public interface ParkrunCourseRepository extends CrudRepository<ParkrunCourse, Long> {
}


Now lets say we want to restrict our ParkrunCourse JPA object, and not return the id or url. We then define our projection as:

@Projection(name = "parkrunCourseExcerpt", types = ParkrunCourse.class)
public interface ParkrunCourseExcerpt {
  String getCourseName();
  Long getAverageTime();
}


We then need to re-define our Spring Data REST repository using the excerptProjection attribute:

@RepositoryRestResource(excerptProjection = ParkrunCourseExcerpt.class)
public interface ParkrunCourseRepository extends CrudRepository<ParkrunCourse, Long> {
}


The projection is then called as:

http://localhost:8080/rest/parkrunCourses/1?projection=parkrunCourseExcerpt

Discussion

This series of posts have demonstrated how Spring Data REST can be used to turn Spring Data repositories into REST APIs. We have also shown how we can control access to these REST API’s using Spring Security or control visibility using RepositoryDetectionStrategies. This post has shown how we can use Projections in a similar way you would use Data Transfer Objects to control what clients see.

Spring Data REST offers a quick way to expose your database as a REST API without lots of boilerplate code. The downside is you need to spend time ensuring the database you have the right level of access exposed, and what information you are intending to expose.

I see two main use cases for this:

  • Exposing an internal database in a decoupled form: You have a number of client applications wanting access to your database and want to ensure access is more loosely coupled than JDBC.
  • Public Access Database: You have a public access database that you wish to expose on the internet. The lack of boilerplate and speed with which you can build your API makes Spring Data JPA a good fit

Conclusions

This post considered how you can use projections to control the view of your Spring Data repositories. We also presented some conclusions on the best use cases for Spring Data REST.

REST Web Protocols Spring Data Spring Framework Data (computing)

Published at DZone with permission of Martin Farrell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Less Code With Spring Data Rest
  • Introduction To Spring Data JPA With Inheritance in A REST Application
  • The Magic of Spring Data
  • Build Reactive REST APIs With Spring WebFlux

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!