@Test public void createInstantiatesObjectMapperWithInlineAssociationDeserializerAwareHandlerInstantiator() { ObjectMapper mapper = new ObjectMapper(); RestTemplate restTemplate = new RestTemplate(); when(mapperFactory.create(any(HandlerInstantiator.class))).thenReturn(mapper); when(restTemplateFactory.create(any(ClientHttpRequestFactory.class), any(ObjectMapper.class))) .thenReturn(restTemplate); factory.create(); ArgumentCaptor<HandlerInstantiator> handlerInstantiator = ArgumentCaptor.forClass(HandlerInstantiator.class); verify(mapperFactory).create(handlerInstantiator.capture()); assertThat(handlerInstantiator.getValue().deserializerInstance(null, null, InlineAssociationDeserializer.class), is(anInlineAssociationDeserializerMatching(aRestOperationsMatching(restTemplate, mapper), proxyFactory))); }
@Test public void createInvokesConfigurerOnObjectMapperIfPresent() { ObjectMapperConfigurer objectMapperConfigurer = mock(ObjectMapperConfigurer.class); Configuration configuration = Configuration.builder() .setObjectMapperConfigurer(objectMapperConfigurer) .build(); ObjectMapper objectMapper = new ObjectMapper(); when(mapperFactory.create(any(HandlerInstantiator.class))) .thenReturn(objectMapper); new RestOperationsFactory(configuration, proxyFactory, mapperFactory, restTemplateFactory) .create(); verify(objectMapperConfigurer).configure(objectMapper); }
@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(DeserializationConfig.class), any(Annotated.class), 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); }
private PropertyNamingStrategy _findNamingStrategy() { Object localObject1; if (this._annotationIntrospector == null) localObject1 = null; else localObject1 = this._annotationIntrospector.findNamingStrategy(this._classDef); Object localObject2 = localObject1; if (localObject1 == null) return this._config.getPropertyNamingStrategy(); if ((localObject2 instanceof PropertyNamingStrategy)) return (PropertyNamingStrategy)localObject2; if (!(localObject2 instanceof Class)) throw new IllegalStateException("AnnotationIntrospector returned PropertyNamingStrategy definition of type " + localObject2.getClass().getName() + "; expected type PropertyNamingStrategy or Class<PropertyNamingStrategy> instead"); Class localClass = (Class)localObject2; if (!PropertyNamingStrategy.class.isAssignableFrom(localClass)) throw new IllegalStateException("AnnotationIntrospector returned Class " + localClass.getName() + "; expected Class<PropertyNamingStrategy>"); HandlerInstantiator localHandlerInstantiator = this._config.getHandlerInstantiator(); if (localHandlerInstantiator != null) { PropertyNamingStrategy localPropertyNamingStrategy = localHandlerInstantiator.namingStrategyInstance(this._config, this._classDef, localClass); if (localPropertyNamingStrategy != null) return localPropertyNamingStrategy; } return (PropertyNamingStrategy)ClassUtil.createInstance(localClass, this._config.canOverrideAccessModifiers()); }
public ValueInstantiator _valueInstantiatorInstance(DeserializationConfig paramDeserializationConfig, Annotated paramAnnotated, Object paramObject) { if (paramObject == null) return null; if ((paramObject instanceof ValueInstantiator)) return (ValueInstantiator)paramObject; if (!(paramObject instanceof Class)) throw new IllegalStateException("AnnotationIntrospector returned key deserializer definition of type " + paramObject.getClass().getName() + "; expected type KeyDeserializer or Class<KeyDeserializer> instead"); Class localClass = (Class)paramObject; if (localClass == NoClass.class) return null; if (!ValueInstantiator.class.isAssignableFrom(localClass)) throw new IllegalStateException("AnnotationIntrospector returned Class " + localClass.getName() + "; expected Class<ValueInstantiator>"); HandlerInstantiator localHandlerInstantiator = paramDeserializationConfig.getHandlerInstantiator(); if (localHandlerInstantiator != null) { ValueInstantiator localValueInstantiator = localHandlerInstantiator.valueInstantiatorInstance(paramDeserializationConfig, paramAnnotated, localClass); if (localValueInstantiator != null) return localValueInstantiator; } return (ValueInstantiator)ClassUtil.createInstance(localClass, paramDeserializationConfig.canOverrideAccessModifiers()); }
@Override public ObjectIdGenerator<?> objectIdGeneratorInstance(Annotated annotated, ObjectIdInfo objectIdInfo) throws JsonMappingException { Class<?> implClass = objectIdInfo.getGeneratorType(); HandlerInstantiator hi = _config.getHandlerInstantiator(); ObjectIdGenerator<?> gen; if (hi != null) { gen = hi.objectIdGeneratorInstance(_config, annotated, implClass); } else { gen = (ObjectIdGenerator<?>) ClassUtil.createInstance(implClass, _config.canOverrideAccessModifiers()); } return gen.forScope(objectIdInfo.getScope()); }
/** * Needed to override JSON De-/Serializer in Jackson. * * @param handlerInstantiator * @return */ @Bean public Jackson2ObjectMapperBuilder jacksonBuilder(HandlerInstantiator handlerInstantiator) { Jackson2ObjectMapperBuilder b = new Jackson2ObjectMapperBuilder(); b.indentOutput(true).mixIn(Workbasket.class, WorkbasketMixIn.class); b.handlerInstantiator(handlerInstantiator); return b; }
@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; }
@Test public void createReturnsRestOperations() { ObjectMapper mapper = new ObjectMapper(); RestTemplate restTemplate = new RestTemplate(); when(mapperFactory.create(any(HandlerInstantiator.class))).thenReturn(mapper); when(restTemplateFactory.create(clientHttpRequestFactory, mapper)).thenReturn(restTemplate); RestOperations restOperations = factory.create(); assertThat(restOperations, is(aRestOperationsMatching(restTemplate, mapper))); }
@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(DeserializationConfig.class), any(Annotated.class), eq(InlineAssociationDeserializer.class)); mapper = new ObjectMapper(); mapper.setHandlerInstantiator(instantiator); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); }
@Before public void setup() { HandlerInstantiator instantiator = mock(HandlerInstantiator.class); doReturn(declaredTypeResourceDeserializer()).when(instantiator).deserializerInstance( any(DeserializationConfig.class), any(Annotated.class), eq(ResourceDeserializer.class)); restTemplate = mock(RestTemplate.class); objectMapper = new DefaultObjectMapperFactory().create(instantiator); restOperations = new RestOperations(restTemplate, objectMapper); }
public JsonSerializer<Object> serializerInstance(Annotated paramAnnotated, Object paramObject) { if (paramObject == null) return null; Object localObject; if ((paramObject instanceof JsonSerializer)) { localObject = (JsonSerializer)paramObject; } else { if (!(paramObject instanceof Class)) throw new IllegalStateException("AnnotationIntrospector returned serializer definition of type " + paramObject.getClass().getName() + "; expected type JsonSerializer or Class<JsonSerializer> instead"); Class localClass = (Class)paramObject; if ((localClass == JsonSerializer.None.class) || (localClass == NoClass.class)) return null; if (!JsonSerializer.class.isAssignableFrom(localClass)) throw new IllegalStateException("AnnotationIntrospector returned Class " + localClass.getName() + "; expected Class<JsonSerializer>"); HandlerInstantiator localHandlerInstantiator = this._config.getHandlerInstantiator(); JsonSerializer localJsonSerializer; if (localHandlerInstantiator == null) localJsonSerializer = null; else localJsonSerializer = localHandlerInstantiator.serializerInstance(this._config, paramAnnotated, localClass); localObject = localJsonSerializer; if (localJsonSerializer == null) localObject = (JsonSerializer)ClassUtil.createInstance(localClass, this._config.canOverrideAccessModifiers()); } return _handleResolvable((JsonSerializer)localObject); }
@Override public JsonSerializer<Object> serializerInstance(Annotated annotated, Object serDef) throws JsonMappingException { if (serDef == null) { return null; } JsonSerializer<?> ser; if (serDef instanceof JsonSerializer) { ser = (JsonSerializer<?>) serDef; } else { /* Alas, there's no way to force return type of "either class * X or Y" -- need to throw an exception after the fact */ if (!(serDef instanceof Class)) { throw new IllegalStateException("AnnotationIntrospector returned serializer definition of type " +serDef.getClass().getName()+"; expected type JsonSerializer or Class<JsonSerializer> instead"); } Class<?> serClass = (Class<?>)serDef; // there are some known "no class" markers to consider too: if (serClass == JsonSerializer.None.class || serClass == NoClass.class) { return null; } if (!JsonSerializer.class.isAssignableFrom(serClass)) { throw new IllegalStateException("AnnotationIntrospector returned Class " +serClass.getName()+"; expected Class<JsonSerializer>"); } HandlerInstantiator hi = _config.getHandlerInstantiator(); if (hi != null) { ser = hi.serializerInstance(_config, annotated, serClass); } else { ser = (JsonSerializer<?>) ClassUtil.createInstance(serClass, _config.canOverrideAccessModifiers()); } } return (JsonSerializer<Object>) _handleResolvable(ser); }
private PropertyNamingStrategy _findNamingStrategy() { Object namingDef = (_annotationIntrospector == null)? null : _annotationIntrospector.findNamingStrategy(_classDef); if (namingDef == null) { return _config.getPropertyNamingStrategy(); } if (namingDef instanceof PropertyNamingStrategy) { return (PropertyNamingStrategy) namingDef; } /* Alas, there's no way to force return type of "either class * X or Y" -- need to throw an exception after the fact */ if (!(namingDef instanceof Class)) { throw new IllegalStateException("AnnotationIntrospector returned PropertyNamingStrategy definition of type " +namingDef.getClass().getName()+"; expected type PropertyNamingStrategy or Class<PropertyNamingStrategy> instead"); } Class<?> namingClass = (Class<?>)namingDef; if (!PropertyNamingStrategy.class.isAssignableFrom(namingClass)) { throw new IllegalStateException("AnnotationIntrospector returned Class " +namingClass.getName()+"; expected Class<PropertyNamingStrategy>"); } HandlerInstantiator hi = _config.getHandlerInstantiator(); if (hi != null) { return hi.namingStrategyInstance(_config, _classDef, namingClass); } return (PropertyNamingStrategy) ClassUtil.createInstance(namingClass, _config.canOverrideAccessModifiers()); }
public ValueInstantiator _valueInstantiatorInstance(DeserializationConfig config, Annotated annotated, Object instDef) throws JsonMappingException { if (instDef == null) { return null; } ValueInstantiator inst; if (instDef instanceof ValueInstantiator) { inst = (ValueInstantiator) instDef; } else { if (!(instDef instanceof Class)) { throw new IllegalStateException("AnnotationIntrospector returned key deserializer definition of type " +instDef.getClass().getName() +"; expected type KeyDeserializer or Class<KeyDeserializer> instead"); } Class<?> instClass = (Class<?>)instDef; if (instClass == NoClass.class) { return null; } if (!ValueInstantiator.class.isAssignableFrom(instClass)) { throw new IllegalStateException("AnnotationIntrospector returned Class "+instClass.getName() +"; expected Class<ValueInstantiator>"); } HandlerInstantiator hi = config.getHandlerInstantiator(); if (hi != null) { inst = hi.valueInstantiatorInstance(config, annotated, instClass); } else { inst = (ValueInstantiator) ClassUtil.createInstance(instClass, config.canOverrideAccessModifiers()); } } // not resolvable or contextual, just return: return inst; }
public final SerializationConfig with(HandlerInstantiator paramHandlerInstantiator) { return _withBase(this._base.withHandlerInstantiator(paramHandlerInstantiator)); }
public final DeserializationConfig with(HandlerInstantiator paramHandlerInstantiator) { return _withBase(this._base.withHandlerInstantiator(paramHandlerInstantiator)); }
@Override public SerializationConfig with(HandlerInstantiator hi) { return _withBase(_base.withHandlerInstantiator(hi)); }
@SuppressWarnings("unchecked") @Override public JsonDeserializer<Object> deserializerInstance(Annotated annotated, Object deserDef) throws JsonMappingException { if (deserDef == null) { return null; } JsonDeserializer<?> deser; if (deserDef instanceof JsonDeserializer) { deser = (JsonDeserializer<?>) deserDef; } else { /* Alas, there's no way to force return type of "either class * X or Y" -- need to throw an exception after the fact */ if (!(deserDef instanceof Class)) { throw new IllegalStateException("AnnotationIntrospector returned deserializer definition of type "+deserDef.getClass().getName()+"; expected type JsonDeserializer or Class<JsonDeserializer> instead"); } Class<?> deserClass = (Class<?>)deserDef; // there are some known "no class" markers to consider too: if (deserClass == JsonDeserializer.None.class || deserClass == NoClass.class) { return null; } if (!JsonDeserializer.class.isAssignableFrom(deserClass)) { throw new IllegalStateException("AnnotationIntrospector returned Class "+deserClass.getName()+"; expected Class<JsonDeserializer>"); } HandlerInstantiator hi = _config.getHandlerInstantiator(); if (hi != null) { deser = hi.deserializerInstance(_config, annotated, deserClass); } else { deser = (JsonDeserializer<?>) ClassUtil.createInstance(deserClass, _config.canOverrideAccessModifiers()); } } // First: need to resolve if (deser instanceof ResolvableDeserializer) { ((ResolvableDeserializer) deser).resolve(this); } return (JsonDeserializer<Object>) deser; }
@Override public final KeyDeserializer keyDeserializerInstance(Annotated annotated, Object deserDef) throws JsonMappingException { if (deserDef == null) { return null; } KeyDeserializer deser; if (deserDef instanceof KeyDeserializer) { deser = (KeyDeserializer) deserDef; } else { if (!(deserDef instanceof Class)) { throw new IllegalStateException("AnnotationIntrospector returned key deserializer definition of type " +deserDef.getClass().getName() +"; expected type KeyDeserializer or Class<KeyDeserializer> instead"); } Class<?> deserClass = (Class<?>)deserDef; // there are some known "no class" markers to consider too: if (deserClass == KeyDeserializer.None.class || deserClass == NoClass.class) { return null; } if (!KeyDeserializer.class.isAssignableFrom(deserClass)) { throw new IllegalStateException("AnnotationIntrospector returned Class "+deserClass.getName() +"; expected Class<KeyDeserializer>"); } HandlerInstantiator hi = _config.getHandlerInstantiator(); if (hi != null) { deser = hi.keyDeserializerInstance(_config, annotated, deserClass); } else { deser = (KeyDeserializer) ClassUtil.createInstance(deserClass, _config.canOverrideAccessModifiers()); } } // First: need to resolve if (deser instanceof ResolvableDeserializer) { ((ResolvableDeserializer) deser).resolve(this); } return deser; }
@Override public DeserializationConfig with(HandlerInstantiator hi) { return _withBase(_base.withHandlerInstantiator(hi)); }
/** * Method for configuring {@link HandlerInstantiator} to use for creating * instances of handlers (such as serializers, deserializers, type and type * id resolvers), given a class. * * @param hi Instantiator to use; if null, use the default implementation */ public Object setHandlerInstantiator(HandlerInstantiator hi) { _deserializationConfig = _deserializationConfig.with(hi); _serializationConfig = _serializationConfig.with(hi); return this; }
/** * Needed for injection into jackson deserilizer. * * @param context * @return */ @Bean public HandlerInstantiator handlerInstantiator(ApplicationContext context) { return new SpringHandlerInstantiator(context.getAutowireCapableBeanFactory()); }
/** * Customize the construction of Jackson handlers * ({@link JsonSerializer}, {@link JsonDeserializer}, {@link KeyDeserializer}, * {@code TypeResolverBuilder} and {@code TypeIdResolver}). * @since 4.1.3 * @see Jackson2ObjectMapperFactoryBean#setApplicationContext(ApplicationContext) */ public void setHandlerInstantiator(HandlerInstantiator handlerInstantiator) { this.builder.handlerInstantiator(handlerInstantiator); }
ObjectMapper create(HandlerInstantiator instantiator);