3

I have an enum class as such:

ONE("1", "Description1"),
TWO("2", "Description2");

String value;
String description;
MyEnum(String value, String description) {
    this.value  = value;
    this.description = description;
}

@Override
public String toString() {
    return this.value;
}
    
@JsonValue
public String value() {
    return this.value;
}

The API I am interacting with is expecting a param with type String and the values can be comma separated.

For example: api.com/test?param1=1,2 I configured a feign client with the url api.com/test And then created a POJO like so

public class POJO {
    private List<MyEnum> param1;
}

And in my feign client I have:

@RequestMapping(method = RequestMethod.GET)
MyResponse getResponse(@SpringQueryMap POJO request);

Is it possible to somehow turn the List of Enums to a List of String before the API call is made via some Spring approach?

As of right now, when I pass a List of Enums, it is only taking into account the last Enum within this list.

UPDATE: I annotated the property I want to convert to a list using @JsonSerialize(converter=abc.class). However @SpringQueryMap doesn't seem to honor that serialization..

Ahmed
  • 121
  • 6
  • 18

2 Answers2

0

Yes is possible, you need to create an interceptor and in that method do the mapping. This topic may be for you.

Spring - Execute code before controller's method is invoked

sourheart
  • 114
  • 1
  • 7
0

So turns out @JsonSerialize was not working with @SpringQueryMap So I did have to add an interceptor.

Like so:

public class MyInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        if(requestTemplate.queries().containsKey("param1")) {
            requestTemplate.query("param1", convert(requestTemplate.queries().get("param1")));
        }
    }
    //convert list to a string
    public String convert(Collection<String> values) {
       final String s = String.join(",", values.stream().map(Object::toString).collect(Collectors.toList()));
       return s;
    }
}

And then in my Feign config class added this:

@Bean
public MyInterceptor myInterceptor() {
    return new MyInterceptor();
}
Ahmed
  • 121
  • 6
  • 18