org.springframework.messaging.converter.MappingJackson2MessageConverter Java Examples

The following examples show how to use org.springframework.messaging.converter.MappingJackson2MessageConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Stomp.java    From WeEvent with Apache License 2.0 7 votes vote down vote up
private void testOverSockJS() throws InterruptedException {
    // sock js transport
    List<Transport> transports = new ArrayList<>(2);
    transports.add(new WebSocketTransport(new StandardWebSocketClient()));
    transports.add(new RestTemplateXhrTransport());

    SockJsClient sockjsClient = new SockJsClient(transports);
    WebSocketStompClient stompClient = new WebSocketStompClient(sockjsClient);

    // StringMessageConverter
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    stompClient.setTaskScheduler(taskScheduler); // for heartbeats

    stompClient.connect(brokerSockJS, getSockJSSessionHandlerAdapter());

    Thread.sleep(100000L);
}
 
Example #2
Source File: UserRegistryMessageHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	MockitoAnnotations.initMocks(this);

	when(this.brokerChannel.send(any())).thenReturn(true);
	this.converter = new MappingJackson2MessageConverter();

	SimpMessagingTemplate brokerTemplate = new SimpMessagingTemplate(this.brokerChannel);
	brokerTemplate.setMessageConverter(this.converter);

	this.localRegistry = mock(SimpUserRegistry.class);
	this.multiServerRegistry = new MultiServerUserRegistry(this.localRegistry);

	this.handler = new UserRegistryMessageHandler(this.multiServerRegistry, brokerTemplate,
			"/topic/simp-user-registry", this.taskScheduler);
}
 
Example #3
Source File: Application.java    From spring-websocket-client with MIT License 6 votes vote down vote up
public static void main(String args[]) throws Exception
   {
WebSocketClient simpleWebSocketClient =
    new StandardWebSocketClient();
List<Transport> transports = new ArrayList<>(1);
transports.add(new WebSocketTransport(simpleWebSocketClient));

SockJsClient sockJsClient = new SockJsClient(transports);
WebSocketStompClient stompClient =
    new WebSocketStompClient(sockJsClient);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());

String url = "ws://localhost:9090/chat";
String userId = "spring-" +
    ThreadLocalRandom.current().nextInt(1, 99);
StompSessionHandler sessionHandler = new MyStompSessionHandler(userId);
StompSession session = stompClient.connect(url, sessionHandler)
    .get();
BufferedReader in =
    new BufferedReader(new InputStreamReader(System.in));
for (;;) {
    System.out.print(userId + " >> ");
    System.out.flush();
    String line = in.readLine();
    if ( line == null ) break;
    if ( line.length() == 0 ) continue;
    ClientMessage msg = new ClientMessage(userId, line);
    session.send("/app/chat/java", msg);
}
   }
 
Example #4
Source File: ServiceClient.java    From spring-boot-websocket-client with MIT License 6 votes vote down vote up
public static void main(String... argv) {
    WebSocketClient webSocketClient = new StandardWebSocketClient();
    WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    stompClient.setTaskScheduler(new ConcurrentTaskScheduler());

    String url = "ws://127.0.0.1:8080/hello";
    StompSessionHandler sessionHandler = new MySessionHandler();
    stompClient.connect(url, sessionHandler);

    new Scanner(System.in).nextLine(); //Don't close immediately.
}
 
Example #5
Source File: SubscriptionMethodReturnValueHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #6
Source File: SpringWebSocketITest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Bean
public CommandLineRunner commandLineRunner() {
  return new CommandLineRunner() {
    @Override
    public void run(final String ... args) throws Exception {
      final String url = "ws://localhost:8080/test-websocket";

      final WebSocketStompClient stompClient = new WebSocketStompClient(new SockJsClient(createTransportClient()));
      stompClient.setMessageConverter(new MappingJackson2MessageConverter());

      final StompSession stompSession = stompClient.connect(url, new StompSessionHandlerAdapter() {
      }).get(10, TimeUnit.SECONDS);

      stompSession.subscribe(SUBSCRIBE_GREETINGS_ENDPOINT, new GreetingStompFrameHandler());
      stompSession.send(SEND_HELLO_MESSAGE_ENDPOINT, "Hello");
    }
  };
}
 
Example #7
Source File: RocketMQMessageConverter.java    From rocketmq-spring with Apache License 2.0 6 votes vote down vote up
public RocketMQMessageConverter() {
    List<MessageConverter> messageConverters = new ArrayList<>();
    ByteArrayMessageConverter byteArrayMessageConverter = new ByteArrayMessageConverter();
    byteArrayMessageConverter.setContentTypeResolver(null);
    messageConverters.add(byteArrayMessageConverter);
    messageConverters.add(new StringMessageConverter());
    if (JACKSON_PRESENT) {
        messageConverters.add(new MappingJackson2MessageConverter());
    }
    if (FASTJSON_PRESENT) {
        try {
            messageConverters.add(
                (MessageConverter)ClassUtils.forName(
                    "com.alibaba.fastjson.support.spring.messaging.MappingFastJsonMessageConverter",
                    ClassUtils.getDefaultClassLoader()).newInstance());
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException ignored) {
            //ignore this exception
        }
    }
    messageConverter = new CompositeMessageConverter(messageConverters);
}
 
Example #8
Source File: UserRegistryMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	MockitoAnnotations.initMocks(this);

	when(this.brokerChannel.send(any())).thenReturn(true);
	this.converter = new MappingJackson2MessageConverter();

	SimpMessagingTemplate brokerTemplate = new SimpMessagingTemplate(this.brokerChannel);
	brokerTemplate.setMessageConverter(this.converter);

	this.localRegistry = mock(SimpUserRegistry.class);
	this.multiServerRegistry = new MultiServerUserRegistry(this.localRegistry);

	this.handler = new UserRegistryMessageHandler(this.multiServerRegistry, brokerTemplate,
			"/topic/simp-user-registry", this.taskScheduler);
}
 
Example #9
Source File: MessageBrokerConfigurationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #10
Source File: MessageBrokerConfigurationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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));
}
 
Example #11
Source File: SubscriptionMethodReturnValueHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example #12
Source File: SqsConfiguration.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
public SqsConfiguration(
		ObjectProvider<SimpleMessageListenerContainerFactory> simpleMessageListenerContainerFactory,
		ObjectProvider<QueueMessageHandlerFactory> queueMessageHandlerFactory,
		BeanFactory beanFactory,
		ObjectProvider<ResourceIdResolver> resourceIdResolver,
		ObjectProvider<MappingJackson2MessageConverter> mappingJackson2MessageConverter,
		ObjectProvider<ObjectMapper> objectMapper) {
	this.simpleMessageListenerContainerFactory = simpleMessageListenerContainerFactory
			.getIfAvailable(SimpleMessageListenerContainerFactory::new);
	this.queueMessageHandlerFactory = queueMessageHandlerFactory
			.getIfAvailable(QueueMessageHandlerFactory::new);
	this.beanFactory = beanFactory;
	this.resourceIdResolver = resourceIdResolver.getIfAvailable();
	this.mappingJackson2MessageConverter = mappingJackson2MessageConverter
			.getIfAvailable();
	this.objectMapper = objectMapper.getIfAvailable();
}
 
Example #13
Source File: AbstractMessageChannelMessagingSendingTemplate.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
protected void initMessageConverter(MessageConverter messageConverter) {

		StringMessageConverter stringMessageConverter = new StringMessageConverter();
		stringMessageConverter.setSerializedPayloadClass(String.class);

		List<MessageConverter> messageConverters = new ArrayList<>();
		messageConverters.add(stringMessageConverter);

		if (messageConverter != null) {
			messageConverters.add(messageConverter);
		}
		else {
			MappingJackson2MessageConverter mappingJackson2MessageConverter = new MappingJackson2MessageConverter();
			mappingJackson2MessageConverter.setSerializedPayloadClass(String.class);
			messageConverters.add(mappingJackson2MessageConverter);
		}

		setMessageConverter(new CompositeMessageConverter(messageConverters));
	}
 
Example #14
Source File: SubscriptionMethodReturnValueHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
}
 
Example #15
Source File: QueueMessageHandlerTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void receiveMessage_methodWithMessageAsParameter_parameterIsConverted() {
	new ApplicationContextRunner()
			.withConfiguration(UserConfigurations
					.of(QueueMessageHandlerWithJacksonMappingConfiguration.class))
			.withBean(IncomingMessageHandlerWithMessageParameter.class)
			.run((context) -> {
				DummyKeyValueHolder messagePayload = new DummyKeyValueHolder("myKey",
						"A value");
				MappingJackson2MessageConverter jsonMapper = context
						.getBean(MappingJackson2MessageConverter.class);
				Message<?> message = jsonMapper.toMessage(messagePayload,
						new MessageHeaders(Collections.singletonMap(
								QueueMessageHandler.LOGICAL_RESOURCE_ID,
								"testQueue")));

				MessageHandler messageHandler = context.getBean(MessageHandler.class);
				messageHandler.handleMessage(message);

				IncomingMessageHandlerWithMessageParameter messageListener = context
						.getBean(IncomingMessageHandlerWithMessageParameter.class);
				assertThat(messageListener.getLastReceivedMessage()).isNotNull();
				assertThat(messageListener.getLastReceivedMessage().getPayload())
						.isEqualTo(messagePayload);
			});
}
 
Example #16
Source File: UserRegistryMessageHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {

	MockitoAnnotations.initMocks(this);

	given(this.brokerChannel.send(any())).willReturn(true);
	this.converter = new MappingJackson2MessageConverter();

	SimpMessagingTemplate brokerTemplate = new SimpMessagingTemplate(this.brokerChannel);
	brokerTemplate.setMessageConverter(this.converter);

	this.localRegistry = mock(SimpUserRegistry.class);
	this.multiServerRegistry = new MultiServerUserRegistry(this.localRegistry);

	this.handler = new UserRegistryMessageHandler(this.multiServerRegistry, brokerTemplate,
			"/topic/simp-user-registry", this.taskScheduler);
}
 
Example #17
Source File: SqsConfigurationTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void configuration_withObjectMapper_shouldSetObjectMapperOnQueueMessageHandler()
		throws Exception {
	// Arrange & Act
	AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
			ConfigurationWithObjectMapper.class);
	QueueMessageHandler queueMessageHandler = applicationContext
			.getBean(QueueMessageHandler.class);
	ObjectMapper objectMapper = applicationContext.getBean(ObjectMapper.class);
	List<MessageConverter> converters = (List<MessageConverter>) ReflectionTestUtils
			.getField(queueMessageHandler, "messageConverters");
	MappingJackson2MessageConverter mappingJackson2MessageConverter = (MappingJackson2MessageConverter) converters
			.get(0);

	// Assert
	assertThat(mappingJackson2MessageConverter.getObjectMapper())
			.isEqualTo(objectMapper);
}
 
Example #18
Source File: MessageBrokerConfigurationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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));
}
 
Example #19
Source File: AbstractMessageBrokerConfiguration.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected MappingJackson2MessageConverter createJacksonConverter() {
	DefaultContentTypeResolver resolver = new DefaultContentTypeResolver();
	resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON);
	MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
	converter.setContentTypeResolver(resolver);
	return converter;
}
 
Example #20
Source File: QueueMessageHandlerFactory.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
private MappingJackson2MessageConverter getDefaultMappingJackson2MessageConverter(
		ObjectMapper objectMapper) {
	MappingJackson2MessageConverter jacksonMessageConverter = new MappingJackson2MessageConverter();
	jacksonMessageConverter.setSerializedPayloadClass(String.class);
	jacksonMessageConverter.setStrictContentTypeMatch(true);

	if (objectMapper != null) {
		jacksonMessageConverter.setObjectMapper(objectMapper);
	}

	return jacksonMessageConverter;
}
 
Example #21
Source File: SpringWebsocketTracingTest.java    From java-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Test
public void testTracedWebsocketSession()
    throws URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
  WebSocketStompClient stompClient = new WebSocketStompClient(
      new SockJsClient(createTransportClient()));
  stompClient.setMessageConverter(new MappingJackson2MessageConverter());

  StompSession stompSession = stompClient.connect(url, new StompSessionHandlerAdapter() {
  }).get(1, TimeUnit.SECONDS);

  stompSession.subscribe(SUBSCRIBE_GREETINGS_ENDPOINT, new GreetingStompFrameHandler());
  stompSession.send(SEND_HELLO_MESSAGE_ENDPOINT, new HelloMessage("Hi"));

  await().until(() -> mockTracer.finishedSpans().size() == 3);
  List<MockSpan> mockSpans = mockTracer.finishedSpans();

  // test same trace
  assertEquals(mockSpans.get(0).context().traceId(), mockSpans.get(1).context().traceId());
  assertEquals(mockSpans.get(0).context().traceId(), mockSpans.get(2).context().traceId());

  List<MockSpan> sendHelloSpans = mockSpans.stream()
      .filter(s -> s.operationName().equals(SEND_HELLO_MESSAGE_ENDPOINT))
      .collect(Collectors.toList());
  List<MockSpan> subscribeGreetingsEndpointSpans = mockSpans.stream().filter(s ->
      s.operationName().equals(SUBSCRIBE_GREETINGS_ENDPOINT))
      .collect(Collectors.toList());
  List<MockSpan> greetingControllerSpans = mockSpans.stream().filter(s ->
      s.operationName().equals(GreetingController.DOING_WORK))
      .collect(Collectors.toList());
  assertEquals(sendHelloSpans.size(), 1);
  assertEquals(subscribeGreetingsEndpointSpans.size(), 1);
  assertEquals(greetingControllerSpans.size(), 1);
  assertEquals(sendHelloSpans.get(0).context().spanId(), subscribeGreetingsEndpointSpans.get(0).parentId());
  assertEquals(sendHelloSpans.get(0).context().spanId(), greetingControllerSpans.get(0).parentId());
}
 
Example #22
Source File: AbstractMessageBrokerConfiguration.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected MappingJackson2MessageConverter createJacksonConverter() {
	DefaultContentTypeResolver resolver = new DefaultContentTypeResolver();
	resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON);
	MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
	converter.setContentTypeResolver(resolver);
	return converter;
}
 
Example #23
Source File: SendToMethodReturnValueHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 = this.getClass().getDeclaredMethod("handleNoAnnotations");
	this.noAnnotationsReturnType = new SynthesizingMethodParameter(method, -1);

	method = this.getClass().getDeclaredMethod("handleAndSendToDefaultDestination");
	this.sendToDefaultDestReturnType = new SynthesizingMethodParameter(method, -1);

	method = this.getClass().getDeclaredMethod("handleAndSendTo");
	this.sendToReturnType = new SynthesizingMethodParameter(method, -1);

	method = this.getClass().getDeclaredMethod("handleAndSendToWithPlaceholders");
	this.sendToWithPlaceholdersReturnType = new SynthesizingMethodParameter(method, -1);

	method = this.getClass().getDeclaredMethod("handleAndSendToUser");
	this.sendToUserReturnType = new SynthesizingMethodParameter(method, -1);

	method = this.getClass().getDeclaredMethod("handleAndSendToUserSingleSession");
	this.sendToUserSingleSessionReturnType = new SynthesizingMethodParameter(method, -1);

	method = this.getClass().getDeclaredMethod("handleAndSendToUserDefaultDestination");
	this.sendToUserDefaultDestReturnType = new SynthesizingMethodParameter(method, -1);

	method = this.getClass().getDeclaredMethod("handleAndSendToUserDefaultDestinationSingleSession");
	this.sendToUserSingleSessionDefaultDestReturnType = new SynthesizingMethodParameter(method, -1);

	method = this.getClass().getDeclaredMethod("handleAndSendToJsonView");
	this.jsonViewReturnType = new SynthesizingMethodParameter(method, -1);
}
 
Example #24
Source File: WebSocketConfig.java    From bearchoke with Apache License 2.0 5 votes vote down vote up
@Override
public boolean configureMessageConverters(List<MessageConverter> converters) {
    MappingJackson2MessageConverter jacksonConverter = new MappingJackson2MessageConverter();
    jacksonConverter.setObjectMapper(objectMapper);
    converters.add(jacksonConverter);

    return true;
}
 
Example #25
Source File: QueueMessagingTemplateTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void instantiation_withoutConverter_shouldAddDefaultJacksonConverterToTheCompositeConverter() {
	// Act
	QueueMessagingTemplate queueMessagingTemplate = new QueueMessagingTemplate(
			createAmazonSqs(), (ResourceIdResolver) null, null);

	// Assert
	assertThat(
			((CompositeMessageConverter) queueMessagingTemplate.getMessageConverter())
					.getConverters()).hasSize(2);
	assertThat(
			((CompositeMessageConverter) queueMessagingTemplate.getMessageConverter())
					.getConverters().get(1))
							.isInstanceOf(MappingJackson2MessageConverter.class);
}
 
Example #26
Source File: MessageBrokerConfigurationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void configureMessageConvertersDefault() {
	AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig();
	CompositeMessageConverter compositeConverter = config.brokerMessageConverter();

	List<MessageConverter> converters = compositeConverter.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());
}
 
Example #27
Source File: MessageSendingTemplateTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test(expected = MessageConversionException.class)
public void convertAndSendNoMatchingConverter() {

	MessageConverter converter = new CompositeMessageConverter(
			Arrays.<MessageConverter>asList(new MappingJackson2MessageConverter()));
	this.template.setMessageConverter(converter);

	this.headers.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_XML);
	this.template.convertAndSend("home", "payload", new MessageHeaders(this.headers));
}
 
Example #28
Source File: WebSocketMessageBrokerConfigurationSupport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #29
Source File: MessageBrokerBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private RuntimeBeanReference registerMessageConverter(Element element, ParserContext context, Object source) {
	Element convertersElement = DomUtils.getChildElementByTagName(element, "message-converters");
	ManagedList<? super Object> converters = new ManagedList<Object>();
	if (convertersElement != null) {
		converters.setSource(source);
		for (Element beanElement : DomUtils.getChildElementsByTagName(convertersElement, "bean", "ref")) {
			Object object = context.getDelegate().parsePropertySubElement(beanElement, null);
			converters.add(object);
		}
	}
	if (convertersElement == null || Boolean.valueOf(convertersElement.getAttribute("register-defaults"))) {
		converters.setSource(source);
		converters.add(new RootBeanDefinition(StringMessageConverter.class));
		converters.add(new RootBeanDefinition(ByteArrayMessageConverter.class));
		if (jackson2Present) {
			RootBeanDefinition jacksonConverterDef = new RootBeanDefinition(MappingJackson2MessageConverter.class);
			RootBeanDefinition resolverDef = new RootBeanDefinition(DefaultContentTypeResolver.class);
			resolverDef.getPropertyValues().add("defaultMimeType", MimeTypeUtils.APPLICATION_JSON);
			jacksonConverterDef.getPropertyValues().add("contentTypeResolver", resolverDef);
			// Use Jackson factory in order to have JSR-310 and Joda-Time modules registered automatically
			GenericBeanDefinition jacksonFactoryDef = new GenericBeanDefinition();
			jacksonFactoryDef.setBeanClass(Jackson2ObjectMapperFactoryBean.class);
			jacksonFactoryDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
			jacksonFactoryDef.setSource(source);
			jacksonConverterDef.getPropertyValues().add("objectMapper", jacksonFactoryDef);
			converters.add(jacksonConverterDef);
		}
	}
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addIndexedArgumentValue(0, converters);
	RootBeanDefinition messageConverterDef = new RootBeanDefinition(CompositeMessageConverter.class, cavs, null);
	return new RuntimeBeanReference(registerBeanDef(messageConverterDef, context, source));
}
 
Example #30
Source File: StompClient.java    From tutorials with MIT License 5 votes vote down vote up
public static void main(String[] args) {
    WebSocketClient client = new StandardWebSocketClient();
    WebSocketStompClient stompClient = new WebSocketStompClient(client);

    stompClient.setMessageConverter(new MappingJackson2MessageConverter());

    StompSessionHandler sessionHandler = new MyStompSessionHandler();
    stompClient.connect(URL, sessionHandler);

    new Scanner(System.in).nextLine(); // Don't close immediately.
}