|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */
10000
div> |
| 16 | + |
| 17 | +package org.springframework.http.support; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.annotation.ObjectIdGenerator; |
| 20 | +import com.fasterxml.jackson.annotation.ObjectIdResolver; |
| 21 | +import org.jspecify.annotations.Nullable; |
| 22 | +import tools.jackson.databind.DeserializationConfig; |
| 23 | +import tools.jackson.databind.KeyDeserializer; |
| 24 | +import tools.jackson.databind.PropertyNamingStrategy; |
| 25 | +import tools.jackson.databind.SerializationConfig; |
| 26 | +import tools.jackson.databind.ValueDeserializer; |
| 27 | +import tools.jackson.databind.ValueSerializer; |
| 28 | +import tools.jackson.databind.cfg.HandlerInstantiator; |
| 29 | +import tools.jackson.databind.cfg.MapperConfig; |
| 30 | +import tools.jackson.databind.deser.ValueInstantiator; |
| 31 | +import tools.jackson.databind.introspect.Annotated; |
| 32 | +import tools.jackson.databind.jsontype.TypeIdResolver; |
| 33 | +import tools.jackson.databind.jsontype.TypeResolverBuilder; |
| 34 | +import tools.jackson.databind.ser.VirtualBeanPropertyWriter; |
| 35 | +import tools.jackson.databind.util.Converter; |
| 36 | + |
| 37 | +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; |
| 38 | +import org.springframework.context.ApplicationContext; |
| 39 | +import org.springframework.util.Assert; |
| 40 | + |
| 41 | +/** |
| 42 | + * Allows for creating Jackson 3.x ({@link ValueSerializer}, {@link ValueDeserializer}, |
| 43 | + * {@link KeyDeserializer}, {@link TypeResolverBuilder}, {@link TypeIdResolver}) |
| 44 | + * beans with autowiring against a Spring {@link ApplicationContext}. |
| 45 | + * |
| 46 | + * <p>Also overrides all factory methods in {@link HandlerInstantiator}, |
| 47 | + * including non-abstract ones for {@link ValueInstantiator}, {@link ObjectIdGenerator}, {@link ObjectIdResolver}, |
| 48 | + * {@link PropertyNamingStrategy}, {@link Converter}, {@link VirtualBeanPropertyWriter}. |
| 49 | + * |
| 50 | + * @author Sebastien Deleuze |
| 51 | + * @since 7.0 |
| 52 | + * @see ApplicationContext#getAutowireCapableBeanFactory() |
| 53 | + * @see tools.jackson.databind.cfg.HandlerInstantiator |
| 54 | + */ |
| 55 | +public class JacksonHandlerInstantiator extends HandlerInstantiator { |
| 56 | + |
| 57 | + private final AutowireCapableBeanFactory beanFactory; |
| 58 | + |
| 59 | + |
| 60 | + /** |
| 61 | + * Create a new AutowiredHandlerInstantiator for the given BeanFactory. |
| 62 | + * @param beanFactory the target BeanFactory |
| 63 | + */ |
| 64 | + public JacksonHandlerInstantiator(AutowireCapableBeanFactory beanFactory) { |
| 65 | + Assert.notNull(beanFactory, "BeanFactory must not be null"); |
| 66 | + this.beanFactory = beanFactory; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + @Nullable |
| 71 | + public ValueDeserializer<?> deserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> deserClass) { |
| 72 | + return (ValueDeserializer<?>) this.beanFactory.createBean(deserClass); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public KeyDeserializer keyDeserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> keyDeserClass) { |
| 77 | + return (KeyDeserializer) this.beanFactory.createBean(keyDeserClass); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public ValueSerializer<?> serializerInstance(SerializationConfig config, Annotated annotated, Class<?> serClass) { |
| 82 | + return (ValueSerializer<?>) this.beanFactory.createBean(serClass); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public TypeResolverBuilder<?> typeResolverBuilderInstance(MapperConfig<?> config, Annotated annotated, Class<?> builderClass) { |
| 87 | + return (TypeResolverBuilder<?>) this.beanFactory.createBean(builderClass); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> resolverClass) { |
| 92 | + return (TypeIdResolver) this.beanFactory.createBean(resolverClass); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) { |
| 97 | + |
| 98 | + return (ValueInstantiator) this.beanFactory.createBean(implClass); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public ObjectIdGenerator<?> objectIdGeneratorIns
D095
tance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) { |
| 103 | + |
| 104 | + return (ObjectIdGenerator<?>) this.beanFactory.createBean(implClass); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public ObjectIdResolver resolverIdGeneratorInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) { |
| 109 | + |
| 110 | + return (ObjectIdResolver) this.beanFactory.createBean(implClass); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public PropertyNamingStrategy namingStrategyInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) { |
| 115 | + |
| 116 | + return (PropertyNamingStrategy) this.beanFactory.createBean(implClass); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public Converter<?, ?> converterInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) { |
| 121 | + return (Converter<?, ?>) this.beanFactory.createBean(implClass); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public VirtualBeanPropertyWriter virtualPropertyWriterInstance(MapperConfig<?> config, Class<?> implClass) { |
| 126 | + return (VirtualBeanPropertyWriter) this.beanFactory.createBean(implClass); |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments