java Feign Client 不解析 Query 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43868680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:47:00  来源:igfitidea点击:

Feign Client does not resolve Query parameter

javahttpnetflix-feignfeign

提问by Bee

Here is my interface.

这是我的界面。

public interface SCIMServiceStub {

    @RequestLine("GET /Users/{id}")
    SCIMUser getUser(@Param("id") String id);

    @RequestLine("GET /Groups?filter=displayName+Eq+{roleName}")
    SCIMGroup isValidRole(@Param("roleName") String roleName);

}

Here getUsercall works fine. But isValidRoleis not working properly as the request is eventually sent like this.

在这里getUser通话工作正常。但isValidRole由于请求最终是这样发送的,因此无法正常工作。

/Groups?filter=displayName+Eq+{roleName}"

Here {roleName}is not resolved. What am I missing here? Appreciate some help, as I'm clueless at this point.

这里{roleName}没有解决。我在这里错过了什么?感谢一些帮助,因为我在这一点上一无所知。

Edit: 1 more question: Is there a way to avoid automatic url encoding of query parameters?

编辑:还有 1 个问题:有没有办法避免查询参数的自动 url 编码?

回答by yongsung.yoon

It seems to be caused by a bug that is already opened - https://github.com/OpenFeign/feign/issues/424

似乎是由已经打开的错误引起的 - https://github.com/OpenFeign/feign/issues/424

Like in comments, you can define your own Param.Expandersomething like below as a workaround.

就像在评论中一样,您可以定义自己的Param.Expander类似下面的内容作为解决方法。

@RequestLine("GET /Groups?filter={roleName}")
String isValidRole(@Param(value = "roleName", expander = PrefixExpander.class) String roleName);

static final class PrefixExpander implements Param.Expander {
    @Override
    public String expand(Object value) {
        return "displayName+Eq+" + value;
    }
}

回答by Frank.Chang

As the recent(2019.04) open feign issueand spring docsay:

正如最近(2019.04)openfeign issuespring doc所说:

The OpenFeign @QueryMap annotation provides support for POJOs to be used as GET parameter maps.

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

OpenFeign @QueryMap 注释支持将 POJO 用作 GET 参数映射。

Spring Cloud OpenFeign 提供了一个等效的@SpringQueryMap 注解,从2.1.0 开始用于将POJO 或Map 参数注解为查询参数映射。

You can use it like this:

你可以这样使用它:

    @GetMapping("user")
    String getUser(@SpringQueryMap User user);
public class User {
    private String name;
    private int age;
    ...
}

回答by Chinthaka Dinadasa

Working fine using @QueryMap

使用正常工作 @QueryMap

URL: /api/v1/task/search?status=PENDING&size=20&page=0

网址:/api/v1/task/search?status=PENDING&size=20&page=0

Map<String, String> parameters = new LinkedHashMap<>()
        parameters.put("status", "PENDING")
        parameters.put("size", "20")
        parameters.put("page", "0")
        def tasks = restClientFactory.taskApiClient.searchTasks(parameters)

Inside Client Interface

内部客户端界面

@RequestLine("GET /api/v1/task/search?{parameters}")
List<Task> searchTasks(@QueryMap parameters)