英文:
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&filter=valueTwo
但它被发送成这样(日志中的请求响应)
filter=[valueOne, valueTwo]
感谢任何帮助。
英文:
I have a nxt request POST with form url encoded using Feign Client
@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;
}
}
}
I want to send the same key with two values
Map<String, Object> param = new HashMap<>();
param.put("filter", valueOne);
param.put("filter", valueTwo);
api.new("asset",param);
I need something like that
filter=valueOne&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 = "nxt", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
String new(@RequestParam String requestType, @RequestParam("filter") List<String> filter, @RequestBody Map<String, ?> payload);
as I found it here: https://stackoverflow.com/questions/41744542/spring-cloud-feign-client-requestparam-with-list-parameter-creates-a-wrong-requ
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论