发送多个按键到Feign客户端

huangapple go评论80阅读模式
英文:

Send multiple key in Feign Client

问题

我有一个使用Feign Client进行表单URL编码的nxt请求POST

@FeignClient(
        url = "${url}", configuration = NxtApi.Configuration.class)
public interface NxtApi {

    @PostMapping(value = "nxt", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    String new(
            @RequestParam String requestType, @RequestBody Map<String, ?> payload);

    class Configuration {

        @Bean
        Encoder feignFormEncoder(ObjectFactory<HttpMessageConverters> converters) {
            return new SpringFormEncoder(new SpringEncoder(converters));
        }

        @Bean
        Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }

    }
}

我想要发送相同的键和两个值

Map<String, Object> param = new HashMap<>();
param.put("filter", valueOne);
param.put("filter", valueTwo);
api.new("asset", param);


**我需要像这样的内容**
filter=valueOne&amp;filter=valueTwo

但它被发送成这样日志中的请求响应
filter=[valueOne, valueTwo]

感谢任何帮助
英文:

I have a nxt request POST with form url encoded using Feign Client

@FeignClient(
        url = &quot;${url}&quot;, configuration = NxtApi.Configuration.class)
public interface NxtApi {

    @PostMapping(value = &quot;nxt&quot;, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    String new(
            @RequestParam String requestType, @RequestBody Map&lt;String, ?&gt; payload);

    class Configuration {

        @Bean
        Encoder feignFormEncoder(ObjectFactory&lt;HttpMessageConverters&gt; converters) {
            return new SpringFormEncoder(new SpringEncoder(converters));
        }

        @Bean
        Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }

    }
}

I want to send the same key with two values

  Map&lt;String, Object&gt; param = new HashMap&lt;&gt;();
  param.put(&quot;filter&quot;, valueOne);
  param.put(&quot;filter&quot;, valueTwo);
  api.new(&quot;asset&quot;,param);

I need something like that

filter=valueOne&amp;filter=valueTwo

But it's being sent like this (Request response in the log)

filter=[valueOne,valueTwo]

Thanks for any help.

答案1

得分: 2

你需要使用一个字符串值的列表(List of String values)来代替一个映射(Map)。

@PostMapping(value = "nxt", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
String new(@RequestParam String requestType, @RequestParam("filter") List<String> filter, @RequestBody Map<String, ?> payload);

我在这里找到了它:https://stackoverflow.com/questions/41744542/spring-cloud-feign-client-requestparam-with-list-parameter-creates-a-wrong-requ

英文:

You will have to use a List of String values instead of a Map.

@PostMapping(value = &quot;nxt&quot;, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
String new(@RequestParam String requestType, @RequestParam(&quot;filter&quot;) List&lt;String&gt; filter, @RequestBody Map&lt;String, ?&gt; payload);

as I found it here: https://stackoverflow.com/questions/41744542/spring-cloud-feign-client-requestparam-with-list-parameter-creates-a-wrong-requ

huangapple
  • 本文由 发表于 2020年10月23日 22:16:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64501718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定