Spring Cloud OpenFeign 创建动态查询参数失败

Spring Cloud OpenFeign Failed to Create Dynamic Query Parameters

Spring cloud openFeign 无法创建动态查询参数。它抛出以下异常,因为 SpringMvcContract 试图找到不存在的 RequestParam 值属性。

java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0


@RequestMapping(method = RequestMethod.GET, value = "/orders")
Pageable<Order> searchOrder2(@RequestParam CustomObject customObject);

我尝试使用 @QueryMap 而不是 @RequestParam,但 @QueryMap 不生成查询参数。

顺便说一句 @RequestParam Map<String, Object> params 方法参数可以很好地生成动态查询参数。

但我想使用一个自定义对象,其中假装客户端可以从对象的属性生成动态查询参数。

spring-cloud-starter-feign 有一个 open issue 用于支持 pojo 对象作为请求参数。因此我使用了一个请求拦截器,它从 feign 方法中获取对象并从其字段中创建 url 的查询部分。感谢@charlesvhe

public class DynamicQueryRequestInterceptor implements RequestInterceptor {

private static final Logger LOGGER = LoggerFactory.getLogger(DynamicQueryRequestInterceptor.class);

private static final String EMPTY = "";

@Autowired
private ObjectMapper objectMapper;

@Override
public void apply(RequestTemplate template) {
    if ("GET".equals(template.method()) && Objects.nonNull(template.body())) {
        try {
            JsonNode jsonNode = objectMapper.readTree(template.body());
            template.body(null);

            Map<String, Collection<String>> queries = new HashMap<>();
            buildQuery(jsonNode, EMPTY, queries);
            template.queries(queries);
        } catch (IOException e) {
            LOGGER.error("IOException occurred while try to create http query");
        }
    }
}

private void buildQuery(JsonNode jsonNode, String path, Map<String, Collection<String>> queries) {
    if (!jsonNode.isContainerNode()) {
        if (jsonNode.isNull()) {
            return;
        }
        Collection<String> values = queries.computeIfAbsent(path, k -> new ArrayList<>());
        values.add(jsonNode.asText());
        return;
    }
    if (jsonNode.isArray()) {
        Iterator<JsonNode> it = jsonNode.elements();
        while (it.hasNext()) {
            buildQuery(it.next(), path, queries);
        }
    } else {
        Iterator<Map.Entry<String, JsonNode>> it = jsonNode.fields();
        while (it.hasNext()) {
            Map.Entry<String, JsonNode> entry = it.next();
            if (StringUtils.hasText(path)) {
                buildQuery(entry.getValue(), path + "." + entry.getKey(), queries);
            } else {
                buildQuery(entry.getValue(), entry.getKey(), queries);
            }
        }
    }
}

}

来自Spring Cloud OpenFeign Docs

Spring Cloud OpenFeign provides an equivalent @SpringQueryMap annotation, which is used to annotate a POJO or Map parameter as a query parameter map

所以你的代码应该是:

@RequestMapping(method = RequestMethod.GET, value = "/orders")
Pageable<Order> searchOrder2(@SpringQueryMap @ModelAttribute CustomObject customObject);