Java Code Examples for org.springframework.messaging.converter.MappingJackson2MessageConverter#setObjectMapper()

The following examples show how to use org.springframework.messaging.converter.MappingJackson2MessageConverter#setObjectMapper() . 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: WebSocketMessageBrokerConfigurationSupport.java    From spring-analysis-note with MIT License 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
	Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();
	ApplicationContext applicationContext = getApplicationContext();
	if (applicationContext != null) {
		builder.applicationContext(applicationContext);
	}
	messageConverter.setObjectMapper(builder.build());
	return messageConverter;
}
 
Example 2
Source File: WebSocketMessageBrokerConfigurationSupport.java    From java-technology-stack with MIT License 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
	Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();
	ApplicationContext applicationContext = getApplicationContext();
	if (applicationContext != null) {
		builder.applicationContext(applicationContext);
	}
	messageConverter.setObjectMapper(builder.build());
	return messageConverter;
}
 
Example 3
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 4
Source File: WebSocketConfig.java    From consensusj with Apache License 2.0 5 votes vote down vote up
@Override
public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
    // Workaround for issue 2445: https://github.com/spring-projects/spring-boot/issues/2445
    DefaultContentTypeResolver resolver = new DefaultContentTypeResolver();
    resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON);
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    converter.setObjectMapper(objectMapper);
    converter.setContentTypeResolver(resolver);
    messageConverters.add(converter);
    return false;
}
 
Example 5
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 6
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 7
Source File: MessagingConfig.java    From blog-tutorials with MIT License 4 votes vote down vote up
@Bean
public MappingJackson2MessageConverter mappingJackson2MessageConverter(@Autowired ObjectMapper objectMapper) {
  MappingJackson2MessageConverter jackson2MessageConverter = new MappingJackson2MessageConverter();
  jackson2MessageConverter.setObjectMapper(objectMapper);
  return jackson2MessageConverter;
}
 
Example 8
Source File: KafkaMessageBrokerConfiguration.java    From piper with Apache License 2.0 4 votes vote down vote up
@Bean
public MessageConverter jacksonMessageConverter(ObjectMapper aObjectMapper) {
  MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter() {
    @Override
    protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
      String type = (String) message.getHeaders().get("_type");
      if (type != null) {
        try {
          targetClass = Class.forName(type);
        } catch (ClassNotFoundException e) {
          logger.warn("Class not found: " + type, e);
        }
      }
      JavaType javaType = getObjectMapper().constructType(targetClass);
      Object payload = message.getPayload();
      Class<?> view = getSerializationView(conversionHint);
      // Note: in the view case, calling withType instead of forType for compatibility with Jackson <2.5
      try {
        if (payload instanceof byte[]) {
          if (view != null) {
            return getObjectMapper().readerWithView(view).forType(javaType).readValue((byte[]) payload);
          } else {
            return getObjectMapper().readValue((byte[]) payload, javaType);
          }
        } else {
          if (view != null) {
            return getObjectMapper().readerWithView(view).forType(javaType).readValue(payload.toString());
          } else {
            return getObjectMapper().readValue(payload.toString(), javaType);
          }
        }
      } catch (IOException ex) {
        throw new MessageConversionException(message, "Could not read JSON: " + ex.getMessage(), ex);
      }
    }

  };
  converter.setObjectMapper(aObjectMapper);

  return converter;
}