Skip to content

Commit ebce9c9

Browse files
update RequestHeader reference and documentation
Signed-off-by: zakaria-shahen <[email protected]>
1 parent f5cf669 commit ebce9c9

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/requestheader.adoc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,60 @@ TIP: Built-in support is available for converting a comma-separated string into
6363
array or collection of strings or other types known to the type conversion system. For
6464
example, a method parameter annotated with `@RequestHeader("Accept")` may be of type
6565
`String` but also of `String[]` or `List<String>`.
66+
67+
We can further remove our dependency on Spring Web MVC by making `@RequestHeader` a meta-annotation on our own annotation. The next example demonstrates how we could do so on an annotation named `@LanguageRequestHeader`.
68+
69+
[tabs]
70+
======
71+
Java::
72+
+
73+
[source,java,indent=0,subs="verbatim,quotes"]
74+
----
75+
@Target({ElementType.PARAMETER, ElementType.TYPE})
76+
@Retention(RetentionPolicy.RUNTIME)
77+
@Documented
78+
@RequestHeader(value = HttpHeaders.ACCEPT_LANGUAGE, defaultValue = "en")
79+
public @interface LanguageRequestHeader {}
80+
----
81+
82+
Kotlin::
83+
+
84+
[source,kotlin,indent=0,subs="verbatim,quotes"]
85+
----
86+
@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.TYPE)
87+
@Retention(AnnotationRetention.RUNTIME)
88+
@MustBeDocumented
89+
@RequestHeader(value = HttpHeaders.ACCEPT_LANGUAGE, defaultValue = "en")
90+
annotation class LanguageRequestHeader
91+
----
92+
======
93+
94+
We have isolated our dependency on Spring MVC to a single file. Now that `@LanguageRequestHeader` has been specified, we can use it to signal to resolve our LanguageRequestHeader of the currently `Accept-Language` request header:
95+
96+
[tabs]
97+
======
98+
Java::
99+
+
100+
[source,java,indent=0,subs="verbatim,quotes"]
101+
----
102+
@GetMapping("/product")
103+
public Flux<Product> fetchProduct(@LanguageRequestHeader String lang) {
104+
105+
// .. fetch product and return them ...
106+
}
107+
----
108+
109+
Kotlin::
110+
+
111+
[source,kotlin,indent=0,subs="verbatim,quotes"]
112+
----
113+
@GetMapping("/product")
114+
fun fetchProduct(@LanguageRequestHeader lang: String): Flux<Product> {
115+
116+
// .. fetch product and return them ...
117+
}
118+
----
119+
======
120+
121+
122+
TIP: We can replace the lang parameter type from `String` to `java.util.Locale` or to our own Language enum, and Spring will handle the conversion of the header value.

framework-docs/modules/ROOT/pages/web/webmvc/mvc-controller/ann-methods/requestheader.adoc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,60 @@ TIP: Built-in support is available for converting a comma-separated string into
6363
array or collection of strings or other types known to the type conversion system. For
6464
example, a method parameter annotated with `@RequestHeader("Accept")` can be of type
6565
`String` but also `String[]` or `List<String>`.
66+
67+
We can further remove our dependency on Spring Web MVC by making `@RequestHeader` a meta-annotation on our own annotation. The next example demonstrates how we could do so on an annotation named `@LanguageRequestHeader`.
68+
69+
[tabs]
70+
======
71+
Java::
72+
+
73+
[source,java,indent=0,subs="verbatim,quotes"]
74+
----
75+
@Target({ElementType.PARAMETER, ElementType.TYPE})
76+
@Retention(RetentionPolicy.RUNTIME)
77+
@Documented
78+
@RequestHeader(value = HttpHeaders.ACCEPT_LANGUAGE, defaultValue = "en")
79+
public @interface LanguageRequestHeader {}
80+
----
81+
82+
Kotlin::
83+
+
84+
[source,kotlin,indent=0,subs="verbatim,quotes"]
85+
----
86+
@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.TYPE)
87+
@Retention(AnnotationRetention.RUNTIME)
88+
@MustBeDocumented
89+
@RequestHeader(value = HttpHeaders.ACCEPT_LANGUAGE, defaultValue = "en")
90+
annotation class LanguageRequestHeader
91+
----
92+
======
93+
94+
We have isolated our dependency on Spring MVC to a single file. Now that `@LanguageRequestHeader` has been specified, we can use it to signal to resolve our LanguageRequestHeader of the currently `Accept-Language` request header:
95+
96+
[tabs]
97+
======
98+
Java::
99+
+
100+
[source,java,indent=0,subs="verbatim,quotes"]
101+
----
102+
@GetMapping("/product")
103+
public List<Product> fetchProduct(@LanguageRequestHeader String lang) {
104+
105+
// .. fetch product and return them ...
106+
}
107+
----
108+
109+
Kotlin::
110+
+
111+
[source,kotlin,indent=0,subs="verbatim,quotes"]
112+
----
113+
@GetMapping("/product")
114+
fun fetchProduct(@LanguageRequestHeader lang: String): List<Product> {
115+
116+
// .. fetch product and return them ...
117+
}
118+
----
119+
======
120+
121+
122+
TIP: We can replace the lang parameter type from `String` to `java.util.Locale` or to our own Language enum, and Spring will handle the conversion of the header value.

spring-web/src/main/java/org/springframework/web/bind/annotation/RequestHeader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
/**
2828
* Annotation which indicates that a method parameter should be bound to a web request header.
2929
*
30-
* <p>Supported for annotated handler methods in Spring MVC and Spring WebFlux.
30+
* <p>Supported for declared as a meta-annotation or directly annotated handler methods
31+
* in Spring MVC and Spring WebFlux.
3132
*
3233
* <p>If the method parameter is {@link java.util.Map Map&lt;String, String&gt;},
3334
* {@link org.springframework.util.MultiValueMap MultiValueMap&lt;String, String&gt;},
@@ -36,6 +37,7 @@
3637
*
3738
* @author Juergen Hoeller
3839
* @author Sam Brannen
40+
* @author Zakaria Shahen
3941
* @since 3.0
4042
* @see RequestMapping
4143
* @see RequestParam

0 commit comments

Comments
 (0)