Browse Source

Support default methods in @HttpExchange interface

Closes gh-28491
pull/28559/head
rstoyanchev 4 years ago
parent
commit
cc56da7735
  1. 15
      spring-web/src/main/java/org/springframework/web/service/invoker/HttpServiceProxyFactory.java
  2. 6
      spring-web/src/test/java/org/springframework/web/service/invoker/HttpServiceMethodTests.java

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

@ -16,6 +16,7 @@ @@ -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; @@ -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 @@ -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);
}
}

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

@ -100,6 +100,8 @@ public class HttpServiceMethodTests { @@ -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 { @@ -228,6 +230,10 @@ public class HttpServiceMethodTests {
@GetExchange
Mono<ResponseEntity<Flux<String>>> getFluxEntity();
default String getDefaultMethodValue() {
return "default value";
}
}

Loading…
Cancel
Save