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
java
public class TimedFetchService { private final ExecutorService executor = Executors.newFixedThreadPool(8); private final HttpClient httpClient = HttpClient.newHttpClient();
Enforcing HTTP timeouts with a Future
concurrency
timeouts
thread-pool
Intermediate
8 steps
java
@Component public class RefreshTokenSuccessHandler implements AuthenticationSuccessHandler { private final RefreshTokenService refreshTokenService;
Issuing JWT and refresh tokens on login in Spring
authentication
jwt
http-cookies
Intermediate
7 steps
java
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.module.SimpleModule;
Serializing Money to JSON in Spring
serialization
jackson
money
Intermediate
8 steps
java
@Component @Order(Ordered.HIGHEST_PRECEDENCE) public class TenantResolutionFilter extends OncePerRequestFilter {
How a tenant-resolution filter works in Spring
multi-tenancy
servlet-filter
thread-local
Intermediate
8 steps
java
@Repository public interface SubscriptionRepository extends JpaRepository<Subscription, Long> { @Modifying(clearAutomatically = true, flushAutomatically = true)
Bulk JPQL updates in a Spring Data repository
jpql
bulk-update
modifying-query
Intermediate
5 steps
java
package com.example.uploads.config; import jakarta.servlet.MultipartConfigElement; import org.springframework.boot.web.servlet.MultipartConfigFactory;
Configuring upload size limits in Spring
file-upload
exception-handling
http-errors
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.