Search in sources :

Example 6 with MessageConverter

use of org.springframework.messaging.converter.MessageConverter in project spring-framework by spring-projects.

the class UserRegistryMessageHandler method handleMessage.

@Override
public void handleMessage(Message<?> message) throws MessagingException {
    MessageConverter converter = this.brokerTemplate.getMessageConverter();
    this.userRegistry.addRemoteRegistryDto(message, converter, getRegistryExpirationPeriod());
}
Also used : MessageConverter(org.springframework.messaging.converter.MessageConverter)

Example 7 with MessageConverter

use of org.springframework.messaging.converter.MessageConverter in project spring-framework by spring-projects.

the class MessageBrokerConfigurationTests method configureMessageConvertersCustomAndDefault.

@Test
public void configureMessageConvertersCustomAndDefault() {
    final MessageConverter testConverter = mock(MessageConverter.class);
    AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig() {

        @Override
        protected boolean configureMessageConverters(List<MessageConverter> messageConverters) {
            messageConverters.add(testConverter);
            return true;
        }
    };
    CompositeMessageConverter compositeConverter = config.brokerMessageConverter();
    assertThat(compositeConverter.getConverters().size(), Matchers.is(4));
    Iterator<MessageConverter> iterator = compositeConverter.getConverters().iterator();
    assertThat(iterator.next(), Matchers.is(testConverter));
    assertThat(iterator.next(), Matchers.instanceOf(StringMessageConverter.class));
    assertThat(iterator.next(), Matchers.instanceOf(ByteArrayMessageConverter.class));
    assertThat(iterator.next(), Matchers.instanceOf(MappingJackson2MessageConverter.class));
}
Also used : StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) ByteArrayMessageConverter(org.springframework.messaging.converter.ByteArrayMessageConverter) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) List(java.util.List) ArrayList(java.util.ArrayList) ByteArrayMessageConverter(org.springframework.messaging.converter.ByteArrayMessageConverter) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) Test(org.junit.Test)

Example 8 with MessageConverter

use of org.springframework.messaging.converter.MessageConverter in project spring-framework by spring-projects.

the class TestValidator method annotationMethodMessageHandler.

@Test
public void annotationMethodMessageHandler() {
    loadBeanDefinitions("websocket-config-broker-simple.xml");
    SimpAnnotationMethodMessageHandler annotationMethodMessageHandler = this.appContext.getBean(SimpAnnotationMethodMessageHandler.class);
    assertNotNull(annotationMethodMessageHandler);
    MessageConverter messageConverter = annotationMethodMessageHandler.getMessageConverter();
    assertNotNull(messageConverter);
    assertTrue(messageConverter instanceof CompositeMessageConverter);
    String name = MessageBrokerBeanDefinitionParser.MESSAGE_CONVERTER_BEAN_NAME;
    CompositeMessageConverter compositeMessageConverter = this.appContext.getBean(name, CompositeMessageConverter.class);
    assertNotNull(compositeMessageConverter);
    name = MessageBrokerBeanDefinitionParser.MESSAGING_TEMPLATE_BEAN_NAME;
    SimpMessagingTemplate simpMessagingTemplate = this.appContext.getBean(name, SimpMessagingTemplate.class);
    assertNotNull(simpMessagingTemplate);
    assertEquals("/personal/", simpMessagingTemplate.getUserDestinationPrefix());
    List<MessageConverter> converters = compositeMessageConverter.getConverters();
    assertThat(converters.size(), Matchers.is(3));
    assertThat(converters.get(0), Matchers.instanceOf(StringMessageConverter.class));
    assertThat(converters.get(1), Matchers.instanceOf(ByteArrayMessageConverter.class));
    assertThat(converters.get(2), Matchers.instanceOf(MappingJackson2MessageConverter.class));
    ContentTypeResolver resolver = ((MappingJackson2MessageConverter) converters.get(2)).getContentTypeResolver();
    assertEquals(MimeTypeUtils.APPLICATION_JSON, ((DefaultContentTypeResolver) resolver).getDefaultMimeType());
    DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(annotationMethodMessageHandler);
    String pathSeparator = (String) new DirectFieldAccessor(handlerAccessor.getPropertyValue("pathMatcher")).getPropertyValue("pathSeparator");
    assertEquals(".", pathSeparator);
}
Also used : StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) SimpMessagingTemplate(org.springframework.messaging.simp.SimpMessagingTemplate) ByteArrayMessageConverter(org.springframework.messaging.converter.ByteArrayMessageConverter) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ContentTypeResolver(org.springframework.messaging.converter.ContentTypeResolver) DefaultContentTypeResolver(org.springframework.messaging.converter.DefaultContentTypeResolver) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) ByteArrayMessageConverter(org.springframework.messaging.converter.ByteArrayMessageConverter) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) Test(org.junit.Test)

Example 9 with MessageConverter

use of org.springframework.messaging.converter.MessageConverter in project spring-boot by spring-projects.

the class WebSocketMessagingAutoConfigurationTests method getCustomizedConverters.

private List<MessageConverter> getCustomizedConverters() {
    List<MessageConverter> customizedConverters = new ArrayList<>();
    WebSocketMessagingAutoConfiguration.WebSocketMessageConverterConfiguration configuration = new WebSocketMessagingAutoConfiguration.WebSocketMessageConverterConfiguration(new ObjectMapper());
    configuration.configureMessageConverters(customizedConverters);
    return customizedConverters;
}
Also used : ArrayList(java.util.ArrayList) SimpleMessageConverter(org.springframework.messaging.converter.SimpleMessageConverter) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 10 with MessageConverter

use of org.springframework.messaging.converter.MessageConverter in project spring-framework by spring-projects.

the class AbstractMessageReceivingTemplate method doConvert.

/**
	 * Convert from the given message to the given target class.
	 * @param message the message to convert
	 * @param targetClass the target class to convert the payload to
	 * @return the converted payload of the reply message (never {@code null})
	 */
@SuppressWarnings("unchecked")
protected <T> T doConvert(Message<?> message, Class<T> targetClass) {
    MessageConverter messageConverter = getMessageConverter();
    T value = (T) messageConverter.fromMessage(message, targetClass);
    if (value == null) {
        throw new MessageConversionException(message, "Unable to convert payload [" + message.getPayload() + "] to type [" + targetClass + "] using converter [" + messageConverter + "]");
    }
    return value;
}
Also used : MessageConversionException(org.springframework.messaging.converter.MessageConversionException) MessageConverter(org.springframework.messaging.converter.MessageConverter)

Aggregations

MessageConverter (org.springframework.messaging.converter.MessageConverter)11 CompositeMessageConverter (org.springframework.messaging.converter.CompositeMessageConverter)7 Test (org.junit.Test)6 ByteArrayMessageConverter (org.springframework.messaging.converter.ByteArrayMessageConverter)6 MappingJackson2MessageConverter (org.springframework.messaging.converter.MappingJackson2MessageConverter)6 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)6 ArrayList (java.util.ArrayList)4 List (java.util.List)2 MessageHeaders (org.springframework.messaging.MessageHeaders)2 ContentTypeResolver (org.springframework.messaging.converter.ContentTypeResolver)2 DefaultContentTypeResolver (org.springframework.messaging.converter.DefaultContentTypeResolver)2 MessageConversionException (org.springframework.messaging.converter.MessageConversionException)2 SimpleMessageConverter (org.springframework.messaging.converter.SimpleMessageConverter)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 Bean (org.springframework.context.annotation.Bean)1 SmartMessageConverter (org.springframework.messaging.converter.SmartMessageConverter)1 InvocableHandlerMethod (org.springframework.messaging.handler.invocation.InvocableHandlerMethod)1 SimpMessagingTemplate (org.springframework.messaging.simp.SimpMessagingTemplate)1 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)1