java
43 lines · 7 steps
How a Spring REST controller maps users
A Spring controller exposes read endpoints that fetch users and convert entities into a clean response record.
Explained by
highlit
1@RestController
2@RequestMapping("/api/users")
3public class UserController {
4
5 private final UserRepository userRepository;
6
7 public UserController(UserRepository userRepository) {
8 this.userRepository = userRepository;
9 }
10
11 @GetMapping("/{id}")
12 public ResponseEntity<UserResponse> getUser(@PathVariable Long id) {
13 return userRepository.findById(id)
14 .map(this::toResponse)
15 .map(ResponseEntity::ok)
16 .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));
17 }
18
19 @GetMapping
20 public List<UserResponse> listUsers() {
21 return userRepository.findAll().stream()
22 .map(this::toResponse)
23 .toList();
24 }
25
26 private UserResponse toResponse(User user) {
27 return new UserResponse(
28 user.getId(),
29 user.getFullName(),
30 user.getEmail(),
31 user.getRole().name(),
32 user.getCreatedAt()
33 );
34 }
35
36 public record UserResponse(
37 Long id,
38 String fullName,
39 String email,
40 String role,
41 Instant createdAt
42 ) {}
43}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Mapping entities to a dedicated response type keeps your API contract decoupled from your database schema.
- 2Chaining Optional's map and orElseThrow turns a missing lookup into a clean HTTP error without null checks.
- 3Constructor injection of a repository makes controllers easy to test and their dependencies explicit.
Related explainers
php
<?php final class UserRepository {
A safe PDO user repository in PHP
prepared-statements
repository-pattern
sql-injection
Intermediate
7 steps
java
import java.util.concurrent.atomic.AtomicInteger; public final class RequestCounter {
A thread-safe request counter with AtomicInteger
concurrency
atomics
lock-free
Intermediate
6 steps
typescript
import { inject } from '@angular/core'; import { CanActivateFn, Router,
Functional route guards in Angular
route-guards
dependency-injection
observables
Intermediate
5 steps
java
@Service public class ReportGenerationService { private final ReportRepository reportRepository;
Async report generation with Spring @Async
async
dependency-injection
completablefuture
Intermediate
7 steps
python
from fastapi import Depends, FastAPI, HTTPException from sqlalchemy import create_engine from sqlalchemy.orm import Session, sessionmaker
Wiring SQLAlchemy sessions into FastAPI
dependency-injection
orm
sessions
Intermediate
7 steps
java
package com.example.billing; import java.math.BigDecimal; import java.math.RoundingMode;
Locale-aware currency formatting in Java
internationalization
rounding
immutability
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-spring-rest-controller-maps-users-explained-java-989d/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.