Add support for serialization in RequestMappingReflectiveProcessor

Support reflection-based serialization of parameters annotated
with @RequestBody and return values annotated with @ResponseBody.

It leverages a new BindingReflectionHintsRegistrar class that
is designed to register transitively the types usually needed
for binding and reflection-based serialization on fields,
constructors and properties. Generics are taken in account
as well.

Closes gh-28518
This commit is contained in:
Sébastien Deleuze
2022-06-13 14:44:49 +02:00
parent 12d756a252
commit a57dead4be
4 changed files with 503 additions and 6 deletions

View File

@@ -0,0 +1,114 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aot.hint.support;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Consumer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aot.hint.ExecutableHint;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
/**
* Register the necessary reflection hints so that the specified type can be bound/serialized at runtime.
* Fields, constructors and property methods are registered, except for a set of types like those in
* {@code java.} package where just the type is registered. Types are discovered transitively and
* generic types are registered as well.
*
* @author Sebastien Deleuze
* @since 6.0
*/
public class BindingReflectionHintsRegistrar {
private static final Log logger = LogFactory.getLog(BindingReflectionHintsRegistrar.class);
private static final Consumer<ExecutableHint.Builder> INVOKE = builder -> builder
.withMode(ExecutableMode.INVOKE);
public void registerReflectionHints(ReflectionHints hints, Type... types) {
for (Type type : types) {
Set<Class<?>> referencedTypes = new LinkedHashSet<>();
collectReferencedTypes(new HashSet<>(), referencedTypes, type);
referencedTypes.forEach(referencedType -> hints.registerType(referencedType, builder -> {
if (shouldRegisterMembers(referencedType)) {
builder.withMembers(MemberCategory.DECLARED_FIELDS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
try {
BeanInfo beanInfo = Introspector.getBeanInfo(referencedType);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
Method writeMethod = propertyDescriptor.getWriteMethod();
if (writeMethod != null && writeMethod.getDeclaringClass() != Object.class) {
hints.registerMethod(writeMethod, INVOKE);
MethodParameter methodParameter = MethodParameter.forExecutable(writeMethod, 0);
registerReflectionHints(hints, methodParameter.getGenericParameterType());
}
Method readMethod = propertyDescriptor.getReadMethod();
if (readMethod != null && readMethod.getDeclaringClass() != Object.class) {
hints.registerMethod(readMethod, INVOKE);
MethodParameter methodParameter = MethodParameter.forExecutable(readMethod, -1);
registerReflectionHints(hints, methodParameter.getGenericParameterType());
}
}
}
catch (IntrospectionException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring referenced type [" + referencedType.getName() + "]: " + ex.getMessage());
}
}
}
}));
}
}
/**
* Return whether the members of the type should be registered transitively.
* @param type the type to evaluate
* @return {@code true} if the members of the type should be registered transitively
*/
protected boolean shouldRegisterMembers(Class<?> type) {
return !type.getCanonicalName().startsWith("java.");
}
private void collectReferencedTypes(Set<Type> seen, Set<Class<?>> types, Type type) {
if (seen.contains(type)) {
return;
}
seen.add(type);
ResolvableType resolvableType = ResolvableType.forType(type);
Class<?> clazz = resolvableType.resolve();
if (clazz != null) {
types.add(clazz);
for (ResolvableType genericResolvableType : resolvableType.getGenerics()) {
collectReferencedTypes(seen, types, genericResolvableType.getType());
}
}
}
}