Support default methods in @HttpExchange interface
Closes gh-28491
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.service.invoker;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
@@ -28,6 +29,7 @@ import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.aop.framework.ReflectiveMethodInvocation;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.EmbeddedValueResolverAware;
|
||||
import org.springframework.core.MethodIntrospector;
|
||||
@@ -215,10 +217,19 @@ public final class HttpServiceProxyFactory implements InitializingBean, Embedded
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object invoke(MethodInvocation invocation) {
|
||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
||||
Method method = invocation.getMethod();
|
||||
HttpServiceMethod httpServiceMethod = this.httpServiceMethods.get(method);
|
||||
return httpServiceMethod.invoke(invocation.getArguments());
|
||||
if (httpServiceMethod != null) {
|
||||
return httpServiceMethod.invoke(invocation.getArguments());
|
||||
}
|
||||
if (method.isDefault()) {
|
||||
if (invocation instanceof ReflectiveMethodInvocation reflectiveMethodInvocation) {
|
||||
Object proxy = reflectiveMethodInvocation.getProxy();
|
||||
return InvocationHandler.invokeDefault(proxy, method, invocation.getArguments());
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Unexpected method invocation: " + method);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -100,6 +100,8 @@ public class HttpServiceMethodTests {
|
||||
Mono<ResponseEntity<Flux<String>>> fluxEntity= service.getFluxEntity();
|
||||
StepVerifier.create(fluxEntity.flatMapMany(HttpEntity::getBody)).expectNext("request", "To", "Entity", "Flux").verifyComplete();
|
||||
verifyClientInvocation("requestToEntityFlux", BODY_TYPE);
|
||||
|
||||
assertThat(service.getDefaultMethodValue()).isEqualTo("default value");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -228,6 +230,10 @@ public class HttpServiceMethodTests {
|
||||
|
||||
@GetExchange
|
||||
Mono<ResponseEntity<Flux<String>>> getFluxEntity();
|
||||
|
||||
default String getDefaultMethodValue() {
|
||||
return "default value";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user