Search in sources :

Example 1 with HandlerMethodArgumentResolver

use of org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver in project spring-framework by spring-projects.

the class AbstractMessageBrokerConfiguration method simpAnnotationMethodMessageHandler.

@Bean
public SimpAnnotationMethodMessageHandler simpAnnotationMethodMessageHandler() {
    SimpAnnotationMethodMessageHandler handler = createAnnotationMethodMessageHandler();
    handler.setDestinationPrefixes(getBrokerRegistry().getApplicationDestinationPrefixes());
    handler.setMessageConverter(brokerMessageConverter());
    handler.setValidator(simpValidator());
    List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
    addArgumentResolvers(argumentResolvers);
    handler.setCustomArgumentResolvers(argumentResolvers);
    List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<>();
    addReturnValueHandlers(returnValueHandlers);
    handler.setCustomReturnValueHandlers(returnValueHandlers);
    PathMatcher pathMatcher = getBrokerRegistry().getPathMatcher();
    if (pathMatcher != null) {
        handler.setPathMatcher(pathMatcher);
    }
    return handler;
}
Also used : HandlerMethodReturnValueHandler(org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler) PathMatcher(org.springframework.util.PathMatcher) ArrayList(java.util.ArrayList) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) HandlerMethodArgumentResolver(org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver) Bean(org.springframework.context.annotation.Bean)

Example 2 with HandlerMethodArgumentResolver

use of org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver in project spring-framework by spring-projects.

the class DefaultMessageHandlerMethodFactoryTests method overrideArgumentResolvers.

@Test
public void overrideArgumentResolvers() throws Exception {
    DefaultMessageHandlerMethodFactory instance = createInstance();
    List<HandlerMethodArgumentResolver> customResolvers = new ArrayList<>();
    customResolvers.add(new CustomHandlerMethodArgumentResolver());
    instance.setArgumentResolvers(customResolvers);
    instance.afterPropertiesSet();
    Message<String> message = MessageBuilder.withPayload("sample").build();
    // This will work as the local resolver is set
    InvocableHandlerMethod invocableHandlerMethod = createInvocableHandlerMethod(instance, "customArgumentResolver", Locale.class);
    invocableHandlerMethod.invoke(message);
    assertMethodInvocation(sample, "customArgumentResolver");
    // This won't work as no resolver is known for the payload
    InvocableHandlerMethod invocableHandlerMethod2 = createInvocableHandlerMethod(instance, "simpleString", String.class);
    thrown.expect(MethodArgumentResolutionException.class);
    thrown.expectMessage("No suitable resolver for");
    invocableHandlerMethod2.invoke(message);
}
Also used : InvocableHandlerMethod(org.springframework.messaging.handler.invocation.InvocableHandlerMethod) ArrayList(java.util.ArrayList) HandlerMethodArgumentResolver(org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver) Test(org.junit.Test)

Example 3 with HandlerMethodArgumentResolver

use of org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver in project spring-framework by spring-projects.

the class MessageBrokerConfigurationTests method customArgumentAndReturnValueTypes.

@Test
public void customArgumentAndReturnValueTypes() throws Exception {
    SimpAnnotationMethodMessageHandler handler = this.customContext.getBean(SimpAnnotationMethodMessageHandler.class);
    List<HandlerMethodArgumentResolver> customResolvers = handler.getCustomArgumentResolvers();
    assertEquals(1, customResolvers.size());
    assertTrue(handler.getArgumentResolvers().contains(customResolvers.get(0)));
    List<HandlerMethodReturnValueHandler> customHandlers = handler.getCustomReturnValueHandlers();
    assertEquals(1, customHandlers.size());
    assertTrue(handler.getReturnValueHandlers().contains(customHandlers.get(0)));
}
Also used : HandlerMethodReturnValueHandler(org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) HandlerMethodArgumentResolver(org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver) Test(org.junit.Test)

Example 4 with HandlerMethodArgumentResolver

use of org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver in project spring-framework by spring-projects.

the class TestValidator method customArgumentAndReturnValueTypes.

@Test
public void customArgumentAndReturnValueTypes() {
    loadBeanDefinitions("websocket-config-broker-custom-argument-and-return-value-types.xml");
    SimpAnnotationMethodMessageHandler handler = this.appContext.getBean(SimpAnnotationMethodMessageHandler.class);
    List<HandlerMethodArgumentResolver> customResolvers = handler.getCustomArgumentResolvers();
    assertEquals(2, customResolvers.size());
    assertTrue(handler.getArgumentResolvers().contains(customResolvers.get(0)));
    assertTrue(handler.getArgumentResolvers().contains(customResolvers.get(1)));
    List<HandlerMethodReturnValueHandler> customHandlers = handler.getCustomReturnValueHandlers();
    assertEquals(2, customHandlers.size());
    assertTrue(handler.getReturnValueHandlers().contains(customHandlers.get(0)));
    assertTrue(handler.getReturnValueHandlers().contains(customHandlers.get(1)));
}
Also used : HandlerMethodReturnValueHandler(org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) HandlerMethodArgumentResolver(org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver) Test(org.junit.Test)

Example 5 with HandlerMethodArgumentResolver

use of org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver in project spring-framework by spring-projects.

the class SimpAnnotationMethodMessageHandler method initArgumentResolvers.

protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
    ConfigurableBeanFactory beanFactory = (getApplicationContext() instanceof ConfigurableApplicationContext ? ((ConfigurableApplicationContext) getApplicationContext()).getBeanFactory() : null);
    List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
    // Annotation-based argument resolution
    resolvers.add(new HeaderMethodArgumentResolver(this.conversionService, beanFactory));
    resolvers.add(new HeadersMethodArgumentResolver());
    resolvers.add(new DestinationVariableMethodArgumentResolver(this.conversionService));
    // Type-based argument resolution
    resolvers.add(new PrincipalMethodArgumentResolver());
    resolvers.add(new MessageMethodArgumentResolver(this.messageConverter));
    resolvers.addAll(getCustomArgumentResolvers());
    resolvers.add(new PayloadArgumentResolver(this.messageConverter, this.validator));
    return resolvers;
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) HeadersMethodArgumentResolver(org.springframework.messaging.handler.annotation.support.HeadersMethodArgumentResolver) ArrayList(java.util.ArrayList) HeaderMethodArgumentResolver(org.springframework.messaging.handler.annotation.support.HeaderMethodArgumentResolver) MessageMethodArgumentResolver(org.springframework.messaging.handler.annotation.support.MessageMethodArgumentResolver) HandlerMethodArgumentResolver(org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver) PayloadArgumentResolver(org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver) DestinationVariableMethodArgumentResolver(org.springframework.messaging.handler.annotation.support.DestinationVariableMethodArgumentResolver)

Aggregations

HandlerMethodArgumentResolver (org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver)7 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)4 HandlerMethodReturnValueHandler (org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler)3 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)3 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)2 InvocableHandlerMethod (org.springframework.messaging.handler.invocation.InvocableHandlerMethod)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 Bean (org.springframework.context.annotation.Bean)1 DestinationVariableMethodArgumentResolver (org.springframework.messaging.handler.annotation.support.DestinationVariableMethodArgumentResolver)1 HeaderMethodArgumentResolver (org.springframework.messaging.handler.annotation.support.HeaderMethodArgumentResolver)1 HeadersMethodArgumentResolver (org.springframework.messaging.handler.annotation.support.HeadersMethodArgumentResolver)1 MessageMethodArgumentResolver (org.springframework.messaging.handler.annotation.support.MessageMethodArgumentResolver)1 PayloadArgumentResolver (org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver)1 PathMatcher (org.springframework.util.PathMatcher)1