Search in sources :

Example 1 with HandlerMethodArgumentResolver

use of org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver in project spring-framework by spring-projects.

the class WebFluxConfigurationSupport method requestMappingHandlerAdapter.

@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
    RequestMappingHandlerAdapter adapter = createRequestMappingHandlerAdapter();
    adapter.setMessageReaders(getMessageReaders());
    adapter.setWebBindingInitializer(getConfigurableWebBindingInitializer());
    adapter.setReactiveAdapterRegistry(webFluxAdapterRegistry());
    List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
    addArgumentResolvers(resolvers);
    if (!resolvers.isEmpty()) {
        adapter.setCustomArgumentResolvers(resolvers);
    }
    return adapter;
}
Also used : ArrayList(java.util.ArrayList) HandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) Bean(org.springframework.context.annotation.Bean)

Example 2 with HandlerMethodArgumentResolver

use of org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver in project spring-framework by spring-projects.

the class ControllerMethodResolver method initResolvers.

private static List<HandlerMethodArgumentResolver> initResolvers(ArgumentResolverConfigurer customResolvers, ReactiveAdapterRegistry adapterRegistry, ConfigurableApplicationContext context, boolean supportDataBinding, List<HttpMessageReader<?>> readers) {
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    boolean requestMappingMethod = !readers.isEmpty() && supportDataBinding;
    // Annotation-based...
    List<HandlerMethodArgumentResolver> result = new ArrayList<>(30);
    result.add(new RequestParamMethodArgumentResolver(beanFactory, adapterRegistry, false));
    result.add(new RequestParamMapMethodArgumentResolver(adapterRegistry));
    result.add(new PathVariableMethodArgumentResolver(beanFactory, adapterRegistry));
    result.add(new PathVariableMapMethodArgumentResolver(adapterRegistry));
    result.add(new MatrixVariableMethodArgumentResolver(beanFactory, adapterRegistry));
    result.add(new MatrixVariableMapMethodArgumentResolver(adapterRegistry));
    if (!readers.isEmpty()) {
        result.add(new RequestBodyMethodArgumentResolver(readers, adapterRegistry));
        result.add(new RequestPartMethodArgumentResolver(readers, adapterRegistry));
    }
    if (supportDataBinding) {
        result.add(new ModelAttributeMethodArgumentResolver(adapterRegistry, false));
    }
    result.add(new RequestHeaderMethodArgumentResolver(beanFactory, adapterRegistry));
    result.add(new RequestHeaderMapMethodArgumentResolver(adapterRegistry));
    result.add(new CookieValueMethodArgumentResolver(beanFactory, adapterRegistry));
    result.add(new ExpressionValueMethodArgumentResolver(beanFactory, adapterRegistry));
    result.add(new SessionAttributeMethodArgumentResolver(beanFactory, adapterRegistry));
    result.add(new RequestAttributeMethodArgumentResolver(beanFactory, adapterRegistry));
    // Type-based...
    if (!readers.isEmpty()) {
        result.add(new HttpEntityMethodArgumentResolver(readers, adapterRegistry));
    }
    result.add(new ModelMethodArgumentResolver(adapterRegistry));
    if (supportDataBinding) {
        result.add(new ErrorsMethodArgumentResolver(adapterRegistry));
    }
    result.add(new ServerWebExchangeMethodArgumentResolver(adapterRegistry));
    result.add(new PrincipalMethodArgumentResolver(adapterRegistry));
    if (requestMappingMethod) {
        result.add(new SessionStatusMethodArgumentResolver());
    }
    result.add(new WebSessionMethodArgumentResolver(adapterRegistry));
    if (KotlinDetector.isKotlinPresent()) {
        result.add(new ContinuationHandlerMethodArgumentResolver());
    }
    // Custom...
    result.addAll(customResolvers.getCustomResolvers());
    // Catch-all...
    result.add(new RequestParamMethodArgumentResolver(beanFactory, adapterRegistry, true));
    if (supportDataBinding) {
        result.add(new ModelAttributeMethodArgumentResolver(adapterRegistry, true));
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) HandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver) SyncHandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 3 with HandlerMethodArgumentResolver

use of org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver in project spring-framework by spring-projects.

the class ControllerMethodResolverTests method modelAttributeArgumentResolvers.

@Test
public void modelAttributeArgumentResolvers() {
    List<InvocableHandlerMethod> methods = this.methodResolver.getModelAttributeMethods(this.handlerMethod);
    assertThat(methods.size()).as("Expected one each from Controller + ControllerAdvice").isEqualTo(2);
    InvocableHandlerMethod invocable = methods.get(0);
    List<HandlerMethodArgumentResolver> resolvers = invocable.getResolvers();
    AtomicInteger index = new AtomicInteger(-1);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestParamMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestParamMapMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(PathVariableMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(PathVariableMapMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(MatrixVariableMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(MatrixVariableMapMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ModelAttributeMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestHeaderMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestHeaderMapMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(CookieValueMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ExpressionValueMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(SessionAttributeMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestAttributeMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ModelMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ErrorsMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ServerWebExchangeMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(PrincipalMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(WebSessionMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ContinuationHandlerMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(CustomArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(CustomSyncArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestParamMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ModelAttributeMethodArgumentResolver.class);
}
Also used : InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver) SyncHandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver) Test(org.junit.jupiter.api.Test)

Example 4 with HandlerMethodArgumentResolver

use of org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver in project spring-framework by spring-projects.

the class ControllerMethodResolverTests method requestMappingArgumentResolvers.

@Test
public void requestMappingArgumentResolvers() {
    InvocableHandlerMethod invocable = this.methodResolver.getRequestMappingMethod(this.handlerMethod);
    List<HandlerMethodArgumentResolver> resolvers = invocable.getResolvers();
    AtomicInteger index = new AtomicInteger(-1);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestParamMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestParamMapMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(PathVariableMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(PathVariableMapMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(MatrixVariableMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(MatrixVariableMapMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestBodyMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestPartMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ModelAttributeMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestHeaderMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestHeaderMapMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(CookieValueMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ExpressionValueMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(SessionAttributeMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestAttributeMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(HttpEntityMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ModelMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ErrorsMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ServerWebExchangeMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(PrincipalMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(SessionStatusMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(WebSessionMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ContinuationHandlerMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(CustomArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(CustomSyncArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(RequestParamMethodArgumentResolver.class);
    assertThat(next(resolvers, index).getClass()).isEqualTo(ModelAttributeMethodArgumentResolver.class);
}
Also used : InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver) SyncHandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver) Test(org.junit.jupiter.api.Test)

Example 5 with HandlerMethodArgumentResolver

use of org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver in project spring-boot by spring-projects.

the class WebFluxAutoConfigurationTests method shouldRegisterCustomHandlerMethodArgumentResolver.

@SuppressWarnings("unchecked")
@Test
void shouldRegisterCustomHandlerMethodArgumentResolver() {
    this.contextRunner.withUserConfiguration(CustomArgumentResolvers.class).run((context) -> {
        RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
        List<HandlerMethodArgumentResolver> customResolvers = (List<HandlerMethodArgumentResolver>) ReflectionTestUtils.getField(adapter.getArgumentResolverConfigurer(), "customResolvers");
        assertThat(customResolvers).contains(context.getBean("firstResolver", HandlerMethodArgumentResolver.class), context.getBean("secondResolver", HandlerMethodArgumentResolver.class));
    });
}
Also used : List(java.util.List) HandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

HandlerMethodArgumentResolver (org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver)6 Test (org.junit.jupiter.api.Test)4 SyncHandlerMethodArgumentResolver (org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 InvocableHandlerMethod (org.springframework.web.reactive.result.method.InvocableHandlerMethod)3 SyncInvocableHandlerMethod (org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod)3 ArrayList (java.util.ArrayList)2 RequestMappingHandlerAdapter (org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter)2 List (java.util.List)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 Bean (org.springframework.context.annotation.Bean)1 ResponseStatusException (org.springframework.web.server.ResponseStatusException)1