Skip to content

Commit cc56da7

Browse files
committed
Support default methods in @HttpExchange interface
Closes gh-28491
1 parent e279856 commit cc56da7

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.service.invoker;
1818

19+
import java.lang.reflect.InvocationHandler;
1920
import java.lang.reflect.Method;
2021
import java.time.Duration;
2122
import java.util.ArrayList;
@@ -28,6 +29,7 @@
2829
import org.aopalliance.intercept.MethodInvocation;
2930

3031
import org.springframework.aop.framework.ProxyFactory;
32+
import org.springframework.aop.framework.ReflectiveMethodInvocation;
3133
import org.springframework.beans.factory.InitializingBean;
3234
import org.springframework.context.EmbeddedValueResolverAware;
3335
import org.springframework.core.MethodIntrospector;
@@ -215,10 +217,19 @@ private HttpServiceMethodInterceptor(List<HttpServiceMethod> methods) {
215217
}
216218

217219
@Override
218-
public Object invoke(MethodInvocation invocation) {
220+
public Object invoke(MethodInvocation invocation) throws Throwable {
219221
Method method = invocation.getMethod();
220222
HttpServiceMethod httpServiceMethod = this.httpServiceMethods.get(method);
221-
return httpServiceMethod.invoke(invocation.getArguments());
223+
if (httpServiceMethod != null) {
224+
return httpServiceMethod.invoke(invocation.getArguments());
225+
}
226+
if (method.isDefault()) {
227+
if (invocation instanceof ReflectiveMethodInvocation reflectiveMethodInvocation) {
228+
Object proxy = reflectiveMethodInvocation.getProxy();
229+
return InvocationHandler.invokeDefault(proxy, method, invocation.getArguments());
230+
}
231+
}
232+
throw new IllegalStateException("Unexpected method invocation: " + method);
222233
}
223234
}
224235

spring-web/src/test/java/org/springframework/web/service/invoker/HttpServiceMethodTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ void reactorService() {
100100
Mono<ResponseEntity<Flux<String>>> fluxEntity= service.getFluxEntity();
101101
StepVerifier.create(fluxEntity.flatMapMany(HttpEntity::getBody)).expectNext("request", "To", "Entity", "Flux").verifyComplete();
102102
verifyClientInvocation("requestToEntityFlux", BODY_TYPE);
103+
104+
assertThat(service.getDefaultMethodValue()).isEqualTo("default value");
103105
}
104106

105107
@Test
@@ -228,6 +230,10 @@ private interface ReactorService {
228230

229231
@GetExchange
230232
Mono<ResponseEntity<Flux<String>>> getFluxEntity();
233+
234+
default String getDefaultMethodValue() {
235+
return "default value";
236+
}
231237
}
232238

233239

0 commit comments

Comments
 (0)