其他分享
首页 > 其他分享> > Spring Cloud OpenFeign无法创建动态查询参数

Spring Cloud OpenFeign无法创建动态查询参数

作者:互联网

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不会生成查询参数.

Btw @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-mvc,spring,spring-boot-2,spring-cloud-2,spring-cloud-feign
来源: https://codeday.me/bug/20190622/1261540.html