Java Code Examples for com.fasterxml.jackson.databind.ObjectMapper#setHandlerInstantiator()

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#setHandlerInstantiator() . 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: ResourceDeserializerTest.java    From bowman with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
	typeResolver = mock(TypeResolver.class);
	configuration = Configuration.build();
	
	instantiator = mock(HandlerInstantiator.class);
	
	doReturn(new ResourceDeserializer(Object.class, typeResolver, configuration))
		.when(instantiator).deserializerInstance(any(), any(), eq(ResourceDeserializer.class));
			
	mapper = new ObjectMapper();
	mapper.setHandlerInstantiator(instantiator);
	mapper.registerModule(new Jackson2HalModule());
	mapper.registerModule(new TestModule());
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	
	doReturn(Object.class).when(typeResolver).resolveType(any(), any(), any());
}
 
Example 2
Source File: DataFlowControllerAutoConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Bean
public SkipperClient skipperClient(SkipperClientProperties properties,
		RestTemplateBuilder restTemplateBuilder, ObjectMapper objectMapper,
		@Nullable OAuth2TokenUtilsService oauth2TokenUtilsService) {

	// TODO (Tzolov) review the manual Hal convertion configuration
	objectMapper.registerModule(new Jackson2HalModule());
	objectMapper.setHandlerInstantiator(new Jackson2HalModule.HalHandlerInstantiator(
			new AnnotationLinkRelationProvider(), CurieProvider.NONE, MessageResolver.DEFAULTS_ONLY, new HalConfiguration()));
	objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

	RestTemplate restTemplate = restTemplateBuilder
			.errorHandler(new SkipperClientResponseErrorHandler(objectMapper))
			.interceptors(new OAuth2AccessTokenProvidingClientHttpRequestInterceptor(oauth2TokenUtilsService))
			.messageConverters(Arrays.asList(new StringHttpMessageConverter(),
					new MappingJackson2HttpMessageConverter(objectMapper)))
			.build();

	return new DefaultSkipperClient(properties.getServerUri(), restTemplate);
}
 
Example 3
Source File: CipherCodecTest.java    From cloud-config with MIT License 6 votes vote down vote up
@Test
public void testJsonSerialize() throws Exception {
    CipherCodec cc = applicationContext.getBean("zk-default-cipher-codec", CipherCodec.class);
    String random = RandomStringUtils.randomAlphabetic(10);
    String encrypted = cc.encode(random, "UTF-8");
    System.out.println("encrypted: "+encrypted);

    BoneCPDataSourceConfig config = new BoneCPDataSourceConfig();
    config.setJdbcUrl("jdbc:h2:mem:b;MODE=MySQL;DB_CLOSE_DELAY=-1");
    config.setUserName("hhe");
    config.setPassword(random);

    ObjectMapper om = new ObjectMapper();
    om.setHandlerInstantiator(new SpringHandlerInstantiator(applicationContext.getAutowireCapableBeanFactory()));

    String result = om.writeValueAsString(config);
    System.out.println(result);
    assertThat(result.indexOf(encrypted)>0, is(true));
    assertThat(result.indexOf(random), is(-1));
}
 
Example 4
Source File: FeignHalAutoConfiguration.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public TypeConstrainedMappingJackson2HttpMessageConverter halJacksonHttpMessageConverter(
		ObjectProvider<ObjectMapper> objectMapper,
		ObjectProvider<HalConfiguration> halConfiguration,
		ObjectProvider<MessageResolver> messageResolver,
		ObjectProvider<CurieProvider> curieProvider,
		ObjectProvider<LinkRelationProvider> linkRelationProvider) {

	ObjectMapper mapper = objectMapper.getIfAvailable(ObjectMapper::new).copy();
	mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

	HalConfiguration configuration = halConfiguration
			.getIfAvailable(HalConfiguration::new);

	CurieProvider curieProviderInstance = curieProvider
			.getIfAvailable(() -> new DefaultCurieProvider(Collections.emptyMap()));

	Jackson2HalModule.HalHandlerInstantiator halHandlerInstantiator = new Jackson2HalModule.HalHandlerInstantiator(
			linkRelationProvider.getIfAvailable(), curieProviderInstance,
			messageResolver.getIfAvailable(), configuration);

	mapper.setHandlerInstantiator(halHandlerInstantiator);

	if (!Jackson2HalModule.isAlreadyRegisteredIn(mapper)) {
		Jackson2HalModule halModule = new Jackson2HalModule();
		mapper.registerModule(halModule);
	}

	TypeConstrainedMappingJackson2HttpMessageConverter converter = new TypeConstrainedMappingJackson2HttpMessageConverter(
			RepresentationModel.class);
	converter.setSupportedMediaTypes(Arrays.asList(HAL_JSON));
	converter.setObjectMapper(mapper);
	return converter;
}
 
Example 5
Source File: DefaultObjectMapperFactory.java    From bowman with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectMapper create(HandlerInstantiator instantiator) {
	ObjectMapper mapper = new ObjectMapper();
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	mapper.registerModule(new Jackson2HalModule());
	mapper.registerModule(new JacksonClientModule());
	mapper.setHandlerInstantiator(instantiator);
	return mapper;
}
 
Example 6
Source File: InlineAssociationDeserializerTest.java    From bowman with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	RestOperations restOperations = mock(RestOperations.class);
	ClientProxyFactory proxyFactory = new JavassistClientProxyFactory();
	
	instantiator = mock(HandlerInstantiator.class);
	
	doReturn(new InlineAssociationDeserializer<>(Object.class, restOperations, proxyFactory))
		.when(instantiator).deserializerInstance(any(), any(), eq(InlineAssociationDeserializer.class));
	
	mapper = new ObjectMapper();
	mapper.setHandlerInstantiator(instantiator);
	mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}