0Pricing
Spring Boot 4 Microservices & REST APIs · Lesson

Projections and DTOs

Select only the data you need.

Projections and DTOs is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Microservices & REST APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Projections

Fetching whole entities when you need only a few columns wastes memory and bandwidth.

Projections let you select a subset of fields, returning lightweight objects instead of full entities.

Interface Projections

Declare an interface with getters for the fields you want. Spring Data implements it as a proxy.

public interface UserSummary {
    String getName();
    String getEmail();
}

Returning a Projection

Use the interface as the query method return type.

public interface UserRepository
    extends JpaRepository<User, Long> {
    List<UserSummary> findByActiveTrue();
}

Closed vs Open Projections

A closed projection only exposes existing properties (efficient — selects only those columns). An open projection computes values with @Value and SpEL (loads the whole entity).

Open Projection Example

Combine fields with a SpEL expression.

public interface UserView {
    @Value("#{target.firstName + ' ' + target.lastName}")
    String getFullName();
}

Class-Based Projections (DTOs)

A DTO class with a matching constructor is another projection style. Spring Data maps query columns to constructor parameters by name.

public class UserDto {
    private final String name;
    private final String email;

    public UserDto(String name, String email) {
        this.name = name;
        this.email = email;
    }
    // getters
}

Returning a DTO

Use the DTO class as the return type, just like interface projections.

public interface UserRepository
    extends JpaRepository<User, Long> {
    List<UserDto> findByActiveTrue();
}

DTOs in JPQL Constructor Expressions

With custom JPQL, use a constructor expression to build DTOs directly.

@Query("SELECT new com.app.UserDto(u.name, u.email) "
     + "FROM User u WHERE u.active = true")
List<UserDto> findActiveSummaries();

Dynamic Projections

Make the return type generic so one method serves multiple projections.

<T> List<T> findByActiveTrue(Class<T> type);

// usage
repo.findByActiveTrue(UserSummary.class);
repo.findByActiveTrue(UserDto.class);

Projections with Native Queries

Interface projections also work with native SQL queries when column names match the getters.

@Query(value = "SELECT name, email FROM users",
       nativeQuery = true)
List<UserSummary> findAllSummaries();

Choosing an Approach

Prefer closed interface projections for read-only lists (most efficient). Use DTO classes when you need behavior or want them outside the persistence layer.

Quick Check

Test your understanding of projections.

Recap

You learned to return lightweight results:

  • Interface projections — closed (efficient) or open (SpEL)
  • Class-based DTO projections via constructor mapping
  • JPQL constructor expressions for custom queries
  • Dynamic projections with a generic type parameter

Frequently asked questions

Is the “Projections and DTOs” lesson free?

Yes — the full text of “Projections and DTOs” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Microservices & REST APIs course, upgrade to CoddyKit PRO.

What will I learn in “Projections and DTOs”?

Select only the data you need. You practise Spring Boot 4 Microservices & REST APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Spring Boot 4 Microservices & REST APIs?

No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Projections and DTOs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Spring Boot 4 Microservices & REST APIs lesson?

Yes. Every Spring Boot 4 Microservices & REST APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Dynamic Queries with Specifications
  2. Projections and DTOs
  3. Auditing with @CreatedDate
  4. Pagination and Sorting
← Back to Spring Boot 4 Microservices & REST APIs