Support suspending handler methods in Spring MVC
This commit adds support for Kotlin Coroutines suspending functions to Spring MVC, by converting those to a Mono that can then be handled by the asynchronous request processing feature. It also optimizes Coroutines detection with the introduction of an optimized KotlinDetector.isSuspendingFunction() method that does not require kotlin-reflect. Closes gh-23611
This commit is contained in:
@@ -38,7 +38,6 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.KotlinDetector;
|
||||
import org.springframework.core.MethodIntrospector;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -606,13 +605,6 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
|
||||
}
|
||||
|
||||
public void register(T mapping, Object handler, Method method) {
|
||||
// Assert that the handler method is not a suspending one.
|
||||
if (KotlinDetector.isKotlinType(method.getDeclaringClass())) {
|
||||
Class<?>[] types = method.getParameterTypes();
|
||||
if ((types.length > 0) && "kotlin.coroutines.Continuation".equals(types[types.length - 1].getName())) {
|
||||
throw new IllegalStateException("Unsupported suspending handler method detected: " + method);
|
||||
}
|
||||
}
|
||||
this.readWriteLock.writeLock().lock();
|
||||
try {
|
||||
HandlerMethod handlerMethod = createHandlerMethod(handler, method);
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
|
||||
/**
|
||||
* No-op resolver for method arguments of type {@link kotlin.coroutines.Continuation}.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.3
|
||||
*/
|
||||
public class ContinuationHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
|
||||
@Override
|
||||
public boolean supportsParameter(MethodParameter parameter) {
|
||||
return "kotlin.coroutines.Continuation".equals(parameter.getParameterType().getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@ import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.core.DefaultParameterNameDiscoverer;
|
||||
import org.springframework.core.KotlinDetector;
|
||||
import org.springframework.core.MethodIntrospector;
|
||||
import org.springframework.core.ParameterNameDiscoverer;
|
||||
import org.springframework.core.ReactiveAdapterRegistry;
|
||||
@@ -670,6 +671,9 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
|
||||
resolvers.add(new ErrorsMethodArgumentResolver());
|
||||
resolvers.add(new SessionStatusMethodArgumentResolver());
|
||||
resolvers.add(new UriComponentsBuilderMethodArgumentResolver());
|
||||
if (KotlinDetector.isKotlinPresent()) {
|
||||
resolvers.add(new ContinuationHandlerMethodArgumentResolver());
|
||||
}
|
||||
|
||||
// Custom arguments
|
||||
if (getCustomArgumentResolvers() != null) {
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.concurrent.Callable;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.core.KotlinDetector;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -271,6 +272,8 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
|
||||
this.returnValue = returnValue;
|
||||
this.returnType = (returnValue instanceof ReactiveTypeHandler.CollectedValuesList ?
|
||||
((ReactiveTypeHandler.CollectedValuesList) returnValue).getReturnType() :
|
||||
KotlinDetector.isSuspendingFunction(super.getMethod()) ?
|
||||
ResolvableType.forMethodParameter(getReturnType()) :
|
||||
ResolvableType.forType(super.getGenericParameterType()).getGeneric());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user