java
30 lines · 7 steps
Header-driven endpoints in a Spring controller
A REST controller reads path variables and request headers, then handles a missing-header error locally.
Explained by
highlit
1@RestController
2@RequestMapping("/api/reports")
3public class ReportController {
4
5 private final ReportService reportService;
6
7 public ReportController(ReportService reportService) {
8 this.reportService = reportService;
9 }
10
11 @GetMapping("/{id}")
12 public ResponseEntity<ReportView> getReport(
13 @PathVariable Long id,
14 @RequestHeader("X-Tenant-Id") String tenantId,
15 @RequestHeader(value = "X-Report-Format", defaultValue = "summary") String format) {
16
17 ReportView view = reportService.render(id, tenantId, format);
18 return ResponseEntity.ok()
19 .header("X-Report-Format", format)
20 .body(view);
21 }
22
23 @ExceptionHandler(MissingRequestHeaderException.class)
24 public ResponseEntity<ApiError> handleMissingHeader(MissingRequestHeaderException ex) {
25 ApiError error = new ApiError(
26 HttpStatus.BAD_REQUEST.value(),
27 "Missing required header: " + ex.getHeaderName());
28 return ResponseEntity.badRequest().body(error);
29 }
30}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Request headers can carry cross-cutting context like tenant identity without polluting the URL path.
- 2Header options with defaults keep some inputs optional while others stay mandatory.
- 3A controller-scoped @ExceptionHandler turns framework exceptions into clean, structured error responses.
Related explainers
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
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/header-driven-endpoints-in-a-spring-controller-explained-java-9e13/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.