Bee
Bee

Reputation: 12512

Feign Client does not resolve Query parameter

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: One more question: Is there a way to avoid automatic url encoding of query parameters?

Upvotes: 15

Views: 62319

Answers (5)

Rahul Soni
Rahul Soni

Reputation: 41

Well, as suggested by @Frank.Chang you can use @SpringQueryMap for this:

@GetMapping("user")
String getUser(@SpringQueryMap User user);
public class User {
    @JsonProperty("na-me")  // json property won't work
    private String name;
    private int age;
    ...
}

But remember, in this case @JsonProperty("na-me"), won't work for you. For that you will have to create a getter for the field and add @Param annotation from feign package on the getter. like this.

public class User {
    private String name;
    private int age;
    ...
    @Param("na-me")
    public String getName() {
        return this.name;
    }

}

Upvotes: 2

hitesh
hitesh

Reputation: 236

With Spring feign client below works fine:

@GetMapping("/Groups?filter=displayName+Eq+{roleName}")
SCIMGroup isValidRole(@PathVariable(value = "roleName") String roleName);

Upvotes: 3

Chinthaka Dinadasa
Chinthaka Dinadasa

Reputation: 3461

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 Map<String, String> parameters)

Upvotes: 12

Frank.Chang
Frank.Chang

Reputation: 993

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;
    ...
}

Upvotes: 30

yongsung.yoon
yongsung.yoon

Reputation: 5589

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;
    }
}

Upvotes: 12

Related Questions