|
| 1 | +/* |
| 2 | + * Copyright 2002-present 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 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.service.invoker; |
| 18 | + |
| 19 | +import org.jspecify.annotations.Nullable; |
| 20 | +import org.springframework.beans.BeanWrapper; |
| 21 | +import org.springframework.beans.PropertyAccessorFactory; |
| 22 | +import org.springframework.core.MethodParameter; |
| 23 | +import org.springframework.core.convert.ConversionService; |
| 24 | +import org.springframework.util.ObjectUtils; |
| 25 | +import org.springframework.web.bind.annotation.BindParam; |
| 26 | +import org.springframework.web.bind.annotation.ModelAttribute; |
| 27 | + |
| 28 | +import java.beans.PropertyDescriptor; |
| 29 | +import java.lang.reflect.Field; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.Collection; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +/** |
| 36 | + * Resolves {@link ModelAttribute}-annotated method parameters by expanding a bean |
| 37 | + * into request parameters for an HTTP client. |
| 38 | + * |
| 39 | + * <p>Behavior: |
| 40 | + * <ul> |
| 41 | + * <li>Each readable bean property yields a request parameter named after the property.</li> |
| 42 | + * <li>{@link BindParam} can override the parameter name. It is supported on both fields and |
| 43 | + * getter methods; if both are present, the getter annotation wins.</li> |
| 44 | + * <li>Null property values are skipped.</li> |
| 45 | + * <li>Values are converted to strings via the configured {@link ConversionService} when |
| 46 | + * possible; otherwise, {@code toString()} is used as a fallback.</li> |
| 47 | + * </ul> |
| 48 | + * |
| 49 | + * @author Hermann Pencole |
| 50 | + * @since 7.0 |
| 51 | + */ |
| 52 | +public class ModelAttributeArgumentResolver extends AbstractNamedValueArgumentResolver { |
| 53 | + private final ConversionService conversionService; |
| 54 | + |
| 55 | + /** |
| 56 | + * Constructor for a resolver to a String value. |
| 57 | + * @param conversionService the {@link ConversionService} to use to format |
| 58 | + * Object to String values |
| 59 | + */ |
| 60 | + public ModelAttributeArgumentResolver(ConversionService conversionService) { |
| 61 | + super(); |
| 62 | + this.conversionService = conversionService; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + @Override |
| 67 | + protected @Nullable NamedValueInfo createNamedValueInfo(MethodParameter parameter) { |
| 68 | + ModelAttribute annot = parameter.getParameterAnnotation(ModelAttribute.class); |
| 69 | + if (annot == null) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + return new NamedValueInfo( |
| 73 | + annot.name(), false, null, "model attribute", |
| 74 | + true); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + protected void addRequestValue(String name, Object argument, MethodParameter parameter, HttpRequestValues.Builder requestValues) { |
| 79 | + // Create a map to store custom parameter names |
| 80 | + Map<String, String> customParamNames = new HashMap<>(); |
| 81 | + |
| 82 | + // Retrieve all @BindParam annotations |
| 83 | + Class<?> clazz = argument.getClass(); |
| 84 | + for (Field field : clazz.getDeclaredFields()) { |
| 85 | + BindParam bindParam = field.getAnnotation(BindParam.class); |
| 86 | + if (bindParam != null) { |
| 87 | + customParamNames.put(field.getName(), bindParam.value()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Convert object to query parameters |
| 92 | + BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(argument); |
| 93 | + for (PropertyDescriptor descriptor : wrapper.getPropertyDescriptors()) { |
| 94 | + String propertyName = descriptor.getName(); |
| 95 | + if (!"class".equals(propertyName)) { |
| 96 | + Object value = wrapper.getPropertyValue(propertyName); |
| 97 | + if (value != null) { |
| 98 | + // Use a custom name if it exists, otherwise use the property name |
| 99 | + String paramName = customParamNames.getOrDefault(propertyName, propertyName); |
| 100 | + requestValues.addRequestParameter(paramName, convertSingleToString(value)); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Convert an arbitrary value to a string using the configured {@link ConversionService} |
| 108 | + * when possible, otherwise falls back to {@code toString()}. |
| 109 | + */ |
| 110 | + private String convertSingleToString(Object value) { |
| 111 | + try { |
| 112 | + if (this.conversionService.canConvert(value.getClass(), String.class)) { |
| 113 | + String converted = this.conversionService.convert(value, String.class); |
| 114 | + return converted != null ? converted : ""; |
| 115 | + } |
| 116 | + } catch (Exception ignore) { |
| 117 | + // Fallback to toString below |
| 118 | + } |
| 119 | + return String.valueOf(value); |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +} |
0 commit comments