org.springframework.messaging.converter.DefaultContentTypeResolver Java Examples
The following examples show how to use
org.springframework.messaging.converter.DefaultContentTypeResolver.
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: AbstractMessageBrokerConfiguration.java From spring-analysis-note with MIT License | 5 votes |
protected MappingJackson2MessageConverter createJacksonConverter() { DefaultContentTypeResolver resolver = new DefaultContentTypeResolver(); resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON); MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setContentTypeResolver(resolver); return converter; }
Example #2
Source File: MessageBrokerConfigurationTests.java From spring-analysis-note with MIT License | 5 votes |
@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 #3
Source File: MessageBrokerBeanDefinitionParser.java From spring-analysis-note with MIT License | 5 votes |
private RuntimeBeanReference registerMessageConverter( Element element, ParserContext context, @Nullable Object source) { Element convertersElement = DomUtils.getChildElementByTagName(element, "message-converters"); ManagedList<Object> converters = new ManagedList<>(); 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 cargs = new ConstructorArgumentValues(); cargs.addIndexedArgumentValue(0, converters); RootBeanDefinition messageConverterDef = new RootBeanDefinition(CompositeMessageConverter.class, cargs, null); String name = MESSAGE_CONVERTER_BEAN_NAME; registerBeanDefByName(name, messageConverterDef, context, source); return new RuntimeBeanReference(name); }
Example #4
Source File: AbstractMessageBrokerConfiguration.java From java-technology-stack with MIT License | 5 votes |
protected MappingJackson2MessageConverter createJacksonConverter() { DefaultContentTypeResolver resolver = new DefaultContentTypeResolver(); resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON); MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setContentTypeResolver(resolver); return converter; }
Example #5
Source File: MessageBrokerConfigurationTests.java From java-technology-stack with MIT License | 5 votes |
@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 #6
Source File: MessageBrokerBeanDefinitionParser.java From java-technology-stack with MIT License | 5 votes |
private RuntimeBeanReference registerMessageConverter( Element element, ParserContext context, @Nullable Object source) { Element convertersElement = DomUtils.getChildElementByTagName(element, "message-converters"); ManagedList<Object> converters = new ManagedList<>(); 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 cargs = new ConstructorArgumentValues(); cargs.addIndexedArgumentValue(0, converters); RootBeanDefinition messageConverterDef = new RootBeanDefinition(CompositeMessageConverter.class, cargs, null); String name = MESSAGE_CONVERTER_BEAN_NAME; registerBeanDefByName(name, messageConverterDef, context, source); return new RuntimeBeanReference(name); }
Example #7
Source File: AbstractMessageBrokerConfiguration.java From spring4-understanding with Apache License 2.0 | 5 votes |
protected MappingJackson2MessageConverter createJacksonConverter() { DefaultContentTypeResolver resolver = new DefaultContentTypeResolver(); resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON); MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setContentTypeResolver(resolver); return converter; }
Example #8
Source File: MessageBrokerConfigurationTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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 #9
Source File: MessageBrokerBeanDefinitionParser.java From spring4-understanding with Apache License 2.0 | 5 votes |
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 #10
Source File: WebSocketConfig.java From consensusj with Apache License 2.0 | 5 votes |
@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 #11
Source File: CompositeMessageConverterFactory.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
/** * @param customConverters a list of {@link AbstractMessageConverter} * @param objectMapper object mapper for for serialization / deserialization */ public CompositeMessageConverterFactory( List<? extends MessageConverter> customConverters, ObjectMapper objectMapper) { this.objectMapper = objectMapper; if (!CollectionUtils.isEmpty(customConverters)) { this.converters = new ArrayList<>(customConverters); } else { this.converters = new ArrayList<>(); } initDefaultConverters(); Field headersField = ReflectionUtils.findField(MessageHeaders.class, "headers"); headersField.setAccessible(true); DefaultContentTypeResolver resolver = new DefaultContentTypeResolver() { @Override @SuppressWarnings("unchecked") public MimeType resolve(@Nullable MessageHeaders headers) { Object contentType = headers.get(MessageHeaders.CONTENT_TYPE); if (contentType instanceof byte[]) { contentType = new String((byte[]) contentType, StandardCharsets.UTF_8); contentType = ((String) contentType).replace("\"", ""); Map<String, Object> headersMap = (Map<String, Object>) ReflectionUtils.getField(headersField, headers); headersMap.put(MessageHeaders.CONTENT_TYPE, contentType); } return super.resolve(headers); } }; resolver.setDefaultMimeType(BindingProperties.DEFAULT_CONTENT_TYPE); this.converters.stream().filter(mc -> mc instanceof AbstractMessageConverter) .forEach(mc -> ((AbstractMessageConverter) mc) .setContentTypeResolver(resolver)); }