java 37 lines · 8 steps

Merging default and per-request headers in Spring

A WebClient filter that layers global default headers under optional per-request overrides on every outbound call.

Explained by highlit
1@Component
2public class HeaderMergeFilter {
3 
4 private static final String PER_REQUEST_HEADERS = HeaderMergeFilter.class.getName() + ".headers";
5 
6 private final Map<String, String> globalHeaders;
7 
8 public HeaderMergeFilter(HttpHeaderProperties properties) {
9 this.globalHeaders = properties.getDefaults();
10 }
11 
12 public ExchangeFilterFunction asFilter() {
13 return (request, next) -> {
14 ClientRequest merged = ClientRequest.from(request)
15 .headers(headers -> {
16 globalHeaders.forEach(headers::putIfAbsent);
17 HttpHeaders overrides = request.attribute(PER_REQUEST_HEADERS)
18 .map(HttpHeaders.class::cast)
19 .orElseGet(HttpHeaders::new);
20 overrides.forEach((name, values) -> headers.put(name, values));
21 })
22 .build();
23 return next.exchange(merged);
24 };
25 }
26 
27 public static Consumer<ClientRequest.Builder> withHeaders(HttpHeaders headers) {
28 return builder -> builder.attribute(PER_REQUEST_HEADERS, headers);
29 }
30 
31 @Bean
32 public WebClient outboundWebClient(WebClient.Builder builder) {
33 return builder
34 .filter(asFilter())
35 .build();
36 }
37}
01 / 01
STEP 01

Walkthrough

Space play ←→ step click any line
Three takeaways
  1. 1Storing per-request data in a request attribute lets you thread configuration through a filter without changing method signatures.
  2. 2putIfAbsent versus put cleanly encodes precedence: defaults fill gaps while overrides win outright.
  3. 3Exposing behavior as an ExchangeFilterFunction keeps header logic in one place and applies it to every WebClient call.

Related explainers

typescript
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
import localeFrExtra from '@angular/common/locales/extra/fr';
import localeDe from '@angular/common/locales/de';

Locale-aware bootstrapping in Angular

i18n localization dependency-injection
Intermediate 8 steps
typescript
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import * as Joi from 'joi';
 

Validating env config at boot in NestJS

configuration schema-validation environment-variables
Intermediate 8 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
typescript
import { Inject, Injectable, Logger } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { InjectRepository } from '@nestjs/typeorm';

A cache-aside country lookup in NestJS

cache-aside dependency-injection batch-lookup
Intermediate 8 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
typescript
import { Injectable, effect, signal, computed } from '@angular/core';
 
interface Preferences {
  theme: 'light' | 'dark';

A signal-based preferences store in Angular

signals state-management persistence
Intermediate 7 steps

Share this explainer

Here's the card — post it anywhere.

Merging default and per-request headers in Spring — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code