private GenericBeanDefinition createObjectMapperFactoryDefinition(Object source) { GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(Jackson2ObjectMapperFactoryBean.class); beanDefinition.setSource(source); beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); return beanDefinition; }
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)); }
/** * Configuration Jackson ObjectMapper * * @return */ @Bean public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter mjhmc = new MappingJackson2HttpMessageConverter(); Jackson2ObjectMapperFactoryBean jomfb = new Jackson2ObjectMapperFactoryBean(); jomfb.setAutoDetectFields(false); jomfb.setIndentOutput(shouldIndentJacksonOutput); jomfb.afterPropertiesSet(); mjhmc.setObjectMapper(jomfb.getObject()); return mjhmc; }
/** * Gets a customized {@link MappingJackson2HttpMessageConverter} to process * payload from TMS Rest API. * * @return */ protected void makeRestTemplateWithCustomObjectMapper(RestTemplate restTemplate) { logger.debug("Getting new rest template"); for (HttpMessageConverter<?> httpMessageConverter : restTemplate.getMessageConverters()) { if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) { MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) httpMessageConverter; // Bug with application/x-spring-data-compact+json: https://jira.spring.io/browse/DATAREST-404 logger.debug("Setting supported media type to just JSON. The Accept header will be updated accordingly to application/json"); mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON)); // This helps with joda deserialize of joda date time string logger.debug("Creating custom jackson2 objectmapper with serialization inclusion changes"); Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean = new Jackson2ObjectMapperFactoryBean(); jackson2ObjectMapperFactoryBean.setSerializationInclusion(JsonInclude.Include.NON_NULL); jackson2ObjectMapperFactoryBean.afterPropertiesSet(); ObjectMapper objectMapper = jackson2ObjectMapperFactoryBean.getObject(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper); } // The default encoding is set to ISO-8559-1 for String type, which is why we have to override it here // For more info: https://jira.spring.io/browse/SPR-9099 if (httpMessageConverter instanceof StringHttpMessageConverter) { StringHttpMessageConverter stringHttpMessageConverter = (StringHttpMessageConverter) httpMessageConverter; stringHttpMessageConverter.setSupportedMediaTypes(Arrays.asList( MediaType.parseMediaType("text/plain;charset=UTF-8"), MediaType.ALL) ); } } }
public static ObjectMapper makeJsonMapper() { Jackson2ObjectMapperFactoryBean factoryBean = new Jackson2ObjectMapperFactoryBean(); factoryBean.setSimpleDateFormat(DATE_FORMAT); factoryBean.setFeaturesToDisable(SerializationFeature.WRITE_NULL_MAP_VALUES); factoryBean.afterPropertiesSet(); return factoryBean.getObject(); }
private ObjectMapper createMapper() throws Exception { final Jackson2ObjectMapperFactoryBean factoryBean = new Jackson2ObjectMapperFactoryBean(); final ServerInstanceSerializer serverInstanceSerializer = new ServerInstanceSerializer(); final ServiceTypeRegistryService serviceTypeRegistryService = mockServiceTypeRegistryService(); serverInstanceSerializer.setServiceTypeRegistryService(serviceTypeRegistryService); factoryBean.setHandlerInstantiator(new TestHandlerInstantiator()); // TODO FIX spring managed object factoryBean.afterPropertiesSet(); return factoryBean.getObject(); }
@Before public void setUp() { factory = new Jackson2ObjectMapperFactoryBean(); }