Skip to content

Commit dcabddd

Browse files
committed
Expose HttpExchange metadata to argument resolvers
See gh-33220
1 parent 0bfc8e2 commit dcabddd

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

spring-web/src/main/java/org/springframework/web/service/invoker/AbstractNamedValueArgumentResolver.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -77,7 +77,7 @@ protected AbstractNamedValueArgumentResolver() {
7777
public boolean resolve(
7878
@Nullable Object argument, MethodParameter parameter, HttpRequestValues.Builder requestValues) {
7979

80-
NamedValueInfo info = getNamedValueInfo(parameter);
80+
NamedValueInfo info = getNamedValueInfo(parameter, requestValues);
8181
if (info == null) {
8282
return false;
8383
}
@@ -102,10 +102,10 @@ public boolean resolve(
102102
}
103103

104104
@Nullable
105-
private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
105+
private NamedValueInfo getNamedValueInfo(MethodParameter parameter, HttpRequestValues.Metadata requestValues) {
106106
NamedValueInfo info = this.namedValueInfoCache.get(parameter);
107107
if (info == null) {
108-
info = createNamedValueInfo(parameter);
108+
info = createNamedValueInfo(parameter, requestValues);
109109
if (info == null) {
110110
return null;
111111
}
@@ -122,6 +122,18 @@ private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
122122
@Nullable
123123
protected abstract NamedValueInfo createNamedValueInfo(MethodParameter parameter);
124124

125+
/**
126+
* Variant of {@link #createNamedValueInfo(MethodParameter)} that also provides
127+
* access to the static values set from {@code @HttpExchange} attributes.
128+
* @since 6.2
129+
*/
130+
@Nullable
131+
protected NamedValueInfo createNamedValueInfo(
132+
MethodParameter parameter, HttpRequestValues.Metadata requestValues) {
133+
134+
return createNamedValueInfo(parameter);
135+
}
136+
125137
private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) {
126138
String name = info.name;
127139
if (info.name.isEmpty()) {

spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,42 @@ public static Builder builder() {
183183
}
184184

185185

186+
/**
187+
* Expose static metadata from {@code @HttpExchange} annotation attributes.
188+
* @since 6.2
189+
*/
190+
public interface Metadata {
191+
192+
/**
193+
* Return the HTTP method, if known.
194+
*/
195+
@Nullable
196+
HttpMethod getHttpMethod();
197+
198+
/**
199+
* Return the URI template, if set already.
200+
*/
201+
@Nullable
202+
String getUriTemplate();
203+
204+
/**
205+
* Return the content type, if set already.
206+
*/
207+
@Nullable
208+
MediaType getContentType();
209+
210+
/**
211+
* Return the acceptable media types, if set already.
212+
*/
213+
@Nullable
214+
List<MediaType> getAcceptMediaTypes();
215+
}
216+
217+
186218
/**
187219
* Builder for {@link HttpRequestValues}.
188220
*/
189-
public static class Builder {
221+
public static class Builder implements Metadata {
190222

191223
@Nullable
192224
private HttpMethod httpMethod;
@@ -357,6 +389,34 @@ public void setBodyValue(@Nullable Object bodyValue) {
357389
this.bodyValue = bodyValue;
358390
}
359391

392+
393+
// Implementation of {@link Metadata} methods
394+
395+
@Override
396+
@Nullable
397+
public HttpMethod getHttpMethod() {
398+
return this.httpMethod;
399+
}
400+
401+
@Override
402+
@Nullable
403+
public String getUriTemplate() {
404+
return this.uriTemplate;
405+
}
406+
407+
@Override
408+
@Nullable
409+
public MediaType getContentType() {
410+
return (this.headers != null ? this.headers.getContentType() : null);
411+
}
412+
413+
@Override
414+
@Nullable
415+
public List<MediaType> getAcceptMediaTypes() {
416+
return (this.headers != null ? this.headers.getAccept() : null);
417+
}
418+
419+
360420
/**
361421
* Build the {@link HttpRequestValues} instance.
362422
*/

0 commit comments

Comments
 (0)