Search in sources :

Example 6 with MappingJackson2MessageConverter

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

the class SendToMethodReturnValueHandlerTests method setup.

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    SimpMessagingTemplate messagingTemplate = new SimpMessagingTemplate(this.messageChannel);
    messagingTemplate.setMessageConverter(new StringMessageConverter());
    this.handler = new SendToMethodReturnValueHandler(messagingTemplate, true);
    this.handlerAnnotationNotRequired = new SendToMethodReturnValueHandler(messagingTemplate, false);
    SimpMessagingTemplate jsonMessagingTemplate = new SimpMessagingTemplate(this.messageChannel);
    jsonMessagingTemplate.setMessageConverter(new MappingJackson2MessageConverter());
    this.jsonHandler = new SendToMethodReturnValueHandler(jsonMessagingTemplate, true);
    Method method = getClass().getDeclaredMethod("handleNoAnnotations");
    this.noAnnotationsReturnType = new SynthesizingMethodParameter(method, -1);
    method = getClass().getDeclaredMethod("handleAndSendToDefaultDestination");
    this.sendToDefaultDestReturnType = new SynthesizingMethodParameter(method, -1);
    method = getClass().getDeclaredMethod("handleAndSendTo");
    this.sendToReturnType = new SynthesizingMethodParameter(method, -1);
    method = getClass().getDeclaredMethod("handleAndSendToWithPlaceholders");
    this.sendToWithPlaceholdersReturnType = new SynthesizingMethodParameter(method, -1);
    method = getClass().getDeclaredMethod("handleAndSendToUser");
    this.sendToUserReturnType = new SynthesizingMethodParameter(method, -1);
    method = getClass().getDeclaredMethod("handleAndSendToUserSingleSession");
    this.sendToUserSingleSessionReturnType = new SynthesizingMethodParameter(method, -1);
    method = getClass().getDeclaredMethod("handleAndSendToUserDefaultDestination");
    this.sendToUserDefaultDestReturnType = new SynthesizingMethodParameter(method, -1);
    method = getClass().getDeclaredMethod("handleAndSendToUserDefaultDestinationSingleSession");
    this.sendToUserSingleSessionDefaultDestReturnType = new SynthesizingMethodParameter(method, -1);
    method = getClass().getDeclaredMethod("handleAndSendToJsonView");
    this.jsonViewReturnType = new SynthesizingMethodParameter(method, -1);
    method = SendToTestBean.class.getDeclaredMethod("handleNoAnnotation");
    this.defaultNoAnnotation = new SynthesizingMethodParameter(method, -1);
    method = SendToTestBean.class.getDeclaredMethod("handleAndSendToDefaultDestination");
    this.defaultEmptyAnnotation = new SynthesizingMethodParameter(method, -1);
    method = SendToTestBean.class.getDeclaredMethod("handleAndSendToOverride");
    this.defaultOverrideAnnotation = new SynthesizingMethodParameter(method, -1);
    method = SendToUserTestBean.class.getDeclaredMethod("handleNoAnnotation");
    this.userDefaultNoAnnotation = new SynthesizingMethodParameter(method, -1);
    method = SendToUserTestBean.class.getDeclaredMethod("handleAndSendToDefaultDestination");
    this.userDefaultEmptyAnnotation = new SynthesizingMethodParameter(method, -1);
    method = SendToUserTestBean.class.getDeclaredMethod("handleAndSendToOverride");
    this.userDefaultOverrideAnnotation = new SynthesizingMethodParameter(method, -1);
}
Also used : StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) SimpMessagingTemplate(org.springframework.messaging.simp.SimpMessagingTemplate) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) Method(java.lang.reflect.Method) Before(org.junit.Before)

Example 7 with MappingJackson2MessageConverter

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

the class SubscriptionMethodReturnValueHandlerTests method setup.

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    SimpMessagingTemplate messagingTemplate = new SimpMessagingTemplate(this.messageChannel);
    messagingTemplate.setMessageConverter(new StringMessageConverter());
    this.handler = new SubscriptionMethodReturnValueHandler(messagingTemplate);
    SimpMessagingTemplate jsonMessagingTemplate = new SimpMessagingTemplate(this.messageChannel);
    jsonMessagingTemplate.setMessageConverter(new MappingJackson2MessageConverter());
    this.jsonHandler = new SubscriptionMethodReturnValueHandler(jsonMessagingTemplate);
    Method method = this.getClass().getDeclaredMethod("getData");
    this.subscribeEventReturnType = new MethodParameter(method, -1);
    method = this.getClass().getDeclaredMethod("getDataAndSendTo");
    this.subscribeEventSendToReturnType = new MethodParameter(method, -1);
    method = this.getClass().getDeclaredMethod("handle");
    this.messageMappingReturnType = new MethodParameter(method, -1);
    method = this.getClass().getDeclaredMethod("getJsonView");
    this.subscribeEventJsonViewReturnType = new MethodParameter(method, -1);
}
Also used : StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) SimpMessagingTemplate(org.springframework.messaging.simp.SimpMessagingTemplate) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) Before(org.junit.Before)

Example 8 with MappingJackson2MessageConverter

use of org.springframework.messaging.converter.MappingJackson2MessageConverter 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 MappingJackson2MessageConverter

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

the class WebSocketMessageBrokerConfigurationSupport method createJacksonConverter.

@Override
protected MappingJackson2MessageConverter createJacksonConverter() {
    MappingJackson2MessageConverter messageConverter = super.createJacksonConverter();
    // Use Jackson builder in order to have JSR-310 and Joda-Time modules registered automatically
    messageConverter.setObjectMapper(Jackson2ObjectMapperBuilder.json().applicationContext(this.getApplicationContext()).build());
    return messageConverter;
}
Also used : MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter)

Aggregations

MappingJackson2MessageConverter (org.springframework.messaging.converter.MappingJackson2MessageConverter)9 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)5 Before (org.junit.Before)4 SimpMessagingTemplate (org.springframework.messaging.simp.SimpMessagingTemplate)4 Test (org.junit.Test)3 CompositeMessageConverter (org.springframework.messaging.converter.CompositeMessageConverter)3 DefaultContentTypeResolver (org.springframework.messaging.converter.DefaultContentTypeResolver)3 MessageConverter (org.springframework.messaging.converter.MessageConverter)3 Method (java.lang.reflect.Method)2 ByteArrayMessageConverter (org.springframework.messaging.converter.ByteArrayMessageConverter)2 ContentTypeResolver (org.springframework.messaging.converter.ContentTypeResolver)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 MethodParameter (org.springframework.core.MethodParameter)1 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)1 MessageHeaders (org.springframework.messaging.MessageHeaders)1 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)1