问题
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 getUser
call works fine. But isValidRole
is not working properly as the request is eventually sent like this.
/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.
Edit: 1 more question: Is there a way to avoid automatic url encoding of query parameters?
回答1:
It seems to be caused by a bug that is already opened - https://github.com/OpenFeign/feign/issues/424
Like in comments, you can define your own Param.Expander
something like below as a workaround.
@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;
}
}
回答2:
As the recent(2019.04) open feign issue and spring doc say:
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.
You can use it like this:
@GetMapping("user")
String getUser(@SpringQueryMap User user);
public class User {
private String name;
private int age;
...
}
回答3:
Working fine using @QueryMap
URL: /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)
来源:https://stackoverflow.com/questions/43868680/feign-client-does-not-resolve-query-parameter