You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Needed this in my project with spring data pageable support and i slightly changed @duzhongyuan 's and @OlgaMaciaszek 's solution: fixed other collection types support, fixed enums and enum collections support. In case if someone will need that:
import feign.Param;
import feign.codec.EncodeException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.openfeign.support.PageableSpringQueryMapEncoder;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.function.Predicate.not;
/**
* CustomNestedObjectQueryMapEncoder
*/
public class CustomNestedObjectQueryMapEncoder extends PageableSpringQueryMapEncoder {
private final Map<Class<?>, CustomNestedObjectQueryMapEncoder.ObjectParamMetadata> classToMetadata = new HashMap<>();
@Override
public Map<String, Object> encode(Object object) throws EncodeException {
if (super.supports(object)) {
return super.encode(object);
}
return encodeInternal(null, object, null);
}
private Map<String, Object> encodeInternal(String prefixName, Object object, Map<String, Object> fieldNameToValue) {
if (null == fieldNameToValue) {
fieldNameToValue = new HashMap<>();
}
try {
CustomNestedObjectQueryMapEncoder.ObjectParamMetadata metadata = getMetadata(object.getClass());
for (Field field : metadata.objectFields) {
Object value = field.get(object);
if (value != null && value != object) {
Param alias = field.getAnnotation(Param.class);
String name = alias != null ? alias.value() : field.getName();
if (StringUtils.isNotBlank(prefixName)) {
name = prefixName + "." + name;
}
Class<?> aClass = value.getClass();
ClassLoader classLoader = aClass.getClassLoader();
if (classLoader == null || aClass.isEnum()) {
processNameAndValue(name, value, fieldNameToValue);
} else {
// Recursive call
encodeInternal(name, value, fieldNameToValue);
}
}
}
return fieldNameToValue;
} catch (IllegalAccessException e) {
throw new EncodeException("Failure encoding object into query map", e);
}
}
private void processNameAndValue(String name, Object value, Map<String, Object> fieldNameToValue) throws IllegalAccessException {
// Determines whether it is a custom object collection
if (isCustomObjectCollection(value)) {
Collection<?> collection = (Collection<?>) value;
int i = 0;
for (Object element : collection) {
ObjectParamMetadata metadata = getMetadata(element.getClass());
for (Field field : metadata.objectFields) {
Object elementValue = field.get(element);
if (elementValue != null && elementValue != element) {
Param alias1 = field.getAnnotation(Param.class);
String elementName = alias1 != null ? alias1.value() : field.getName();
elementName = name + "[" + i + "]." + elementName;
ClassLoader classLoader1 = elementValue.getClass().getClassLoader();
if (classLoader1 == null) {
if (isCustomObjectCollection(elementValue)) {
// Recursive call
processNameAndValue(elementName, elementValue, fieldNameToValue);
} else {
fieldNameToValue.put(elementName, elementValue);
}
}
}
}
i++;
}
} else {
fieldNameToValue.put(name, value);
}
}
private boolean isCustomObjectCollection(Object value) {
return Optional.ofNullable(value)
.filter(Collection.class::isInstance)
.map(Collection.class::cast)
.filter(not(Collection::isEmpty))
.stream()
.flatMap(collection -> (Stream<?>) collection.stream())
.filter(not(Enum.class::isInstance))
.map(Object::getClass)
.map(Class::getClassLoader)
.anyMatch(Objects::nonNull);
}
private CustomNestedObjectQueryMapEncoder.ObjectParamMetadata getMetadata(Class<?> objectType) {
CustomNestedObjectQueryMapEncoder.ObjectParamMetadata metadata = classToMetadata.get(objectType);
if (metadata == null) {
metadata = CustomNestedObjectQueryMapEncoder.ObjectParamMetadata.parseObjectType(objectType);
classToMetadata.put(objectType, metadata);
}
return metadata;
}
private static class ObjectParamMetadata {
private final List<Field> objectFields;
private ObjectParamMetadata(List<Field> objectFields) {
this.objectFields = Collections.unmodifiableList(objectFields);
}
private static CustomNestedObjectQueryMapEncoder.ObjectParamMetadata parseObjectType(Class<?> type) {
List<Field> allFields = new ArrayList<>();
for (Class<?> currentClass = type; currentClass != null && !currentClass.isEnum(); currentClass =
currentClass.getSuperclass()) {
Collections.addAll(allFields, currentClass.getDeclaredFields());
}
return new CustomNestedObjectQueryMapEncoder.ObjectParamMetadata(allFields.stream()
.filter(field -> !field.isSynthetic())
.peek(field -> field.setAccessible(true))
.collect(Collectors.toList()));
}
}
}
Usage:
@Autowired(required = false)
private SpringDataWebProperties springDataWebProperties;
@Bean
public QueryMapEncoder feignQueryMapEncoderPageable() {
PageableSpringQueryMapEncoder queryMapEncoder = new CustomNestedObjectQueryMapEncoder();
if (springDataWebProperties != null) {
queryMapEncoder.setPageParameter(springDataWebProperties.getPageable().getPageParameter());
queryMapEncoder.setSizeParameter(springDataWebProperties.getPageable().getSizeParameter());
queryMapEncoder.setSortParameter(springDataWebProperties.getSort().getSortParameter());
}
return queryMapEncoder;
}
No, @Wuaner , it's an enhancement that is marked as "help wanted". That means we are not planning to work on it, but we are happy to review community PRs for it if they are submitted.
I'd like to take this. Before any PR, one thing needs settling.
The flatten itself is straightforward: nested objects to dot notation (range.from=1&range.to=2) and object collections to list[0].field=..., same as the workarounds above. It fits in PageableSpringQueryMapEncoder, so feign-core stays untouched.
The open question is the opt-in. There's no major release planned, so this has to stay backwards compatible: I'd gate it behind a property defaulting to off, same reasoning as #1013. @OlgaMaciaszek do you want a dedicated property here, or should this build on the @QueryMap mapEncoder attribute from #1013? That direction is the only blocker before I start.
I have opened #1448 for this. It follows the direction from the discussion: an opt-in flag, spring.cloud.openfeign.client.nested-query-map-enabled (disabled by default), and no new interface. When it is set, the auto-configured QueryMapEncoder is decorated with a NestedObjectQueryMapEncoder that sends a nested object as range.from=1&range.to=2, the objects of a collection or an array as filters[0].name=status, and the entries of a map as attributes[color]=red, which Spring MVC binds with @ModelAttribute. Collections of simple values are still sent as repeated parameters. The PR includes an end-to-end test against a Spring MVC controller, and the decorator can also wrap a custom QueryMapEncoder.
Activity
o2-mesmer commentedon Feb 11, 2021
@zxy-c You can define a custom
feign.QueryMapEncoderand wire it through the FeignClient'sconfiguration.duzhongyuan commentedon Oct 9, 2021
For nested object, Collection nested objects. Rewrite
FieldQueryMapEncoder .javaOlgaMaciaszek commentedon Feb 14, 2022
@duzhongyuan Would you like to provide a PR?
yulgutlin commentedon May 31, 2022
Needed this in my project with spring data pageable support and i slightly changed @duzhongyuan 's and @OlgaMaciaszek 's solution: fixed other collection types support, fixed enums and enum collections support. In case if someone will need that:
Usage:
Wuaner commentedon Jan 12, 2024
Is this fixed in any followup releases?
OlgaMaciaszek commentedon Jan 12, 2024
No, @Wuaner , it's an enhancement that is marked as "help wanted". That means we are not planning to work on it, but we are happy to review community PRs for it if they are submitted.
nikita-kibitkin commentedon May 18, 2026
I'd like to take this. Before any PR, one thing needs settling.
The flatten itself is straightforward: nested objects to dot notation (range.from=1&range.to=2) and object collections to list[0].field=..., same as the workarounds above. It fits in PageableSpringQueryMapEncoder, so feign-core stays untouched.
The open question is the opt-in. There's no major release planned, so this has to stay backwards compatible: I'd gate it behind a property defaulting to off, same reasoning as #1013. @OlgaMaciaszek do you want a dedicated property here, or should this build on the @QueryMap mapEncoder attribute from #1013? That direction is the only blocker before I start.
sharanggupta commentedon Oct 13, 2026
I have opened #1448 for this. It follows the direction from the discussion: an opt-in flag,
spring.cloud.openfeign.client.nested-query-map-enabled(disabled by default), and no new interface. When it is set, the auto-configuredQueryMapEncoderis decorated with aNestedObjectQueryMapEncoderthat sends a nested object asrange.from=1&range.to=2, the objects of a collection or an array asfilters[0].name=status, and the entries of a map asattributes[color]=red, which Spring MVC binds with@ModelAttribute. Collections of simple values are still sent as repeated parameters. The PR includes an end-to-end test against a Spring MVC controller, and the decorator can also wrap a customQueryMapEncoder.Send the nested objects of @SpringQueryMap as nested query parameters
Merge pull request #1448 from sharanggupta/gh-442-nested-query-map