如何解决在 Feign Client 中使用弹簧形式编码器时,请求正文未正确编码和隐藏
我已经为 spring open feign 添加了必要的依赖项,如 https://github.com/OpenFeign/feign-form 中提到的,并遵循了提到的 feign-client 配置。
每当我发送内容类型为 application/x-www-form-urlencoded 的 post 请求时。请求正文未正确生成。
电子邮件客户端.java
@FeignClient(name = "email",url = "localhost:3000",configuration = EmailClientConfiguration.class)
public interface EmailClient {
@PostMapping(value = "/email/send")
ResponseDto sendEmail(@RequestBody Map<String,String> requestBody);
}
这是我的客户端配置类:
public class EmailClientConfiguration {
@Bean
public RequestInterceptor requestInterceptor(Account<Account> account) {
return template -> {
template.header("Content-Type","application/x-www-form-urlencoded");
};
}
@Bean
public OkHttpClient client() {
return new OkHttpClient();
}
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
@Bean
public Decoder feignDecoder() {
return new JacksonDecoder();
}
@Bean
public Encoder feignFormEncoder () {
return new SpringFormEncoder(new JacksonEncoder());
}
}
Map<String,String> requestBody = new HashMap<>();
requestBody.put("username","xyz");
requestBody.put("email","xyz@gmail.com");
requestBody.put("key","xxx");
当我在接口中调用 sendEmail 方法时,请求者标头设置正确,但请求正文发送为
{"{\n \"key\" : \"xxx\",\n \"email\" : \"xyz@gmail.com\",\n \"username\" : \"xyz\"\n}"
有人可以帮忙解决这个问题吗?为什么请求正文是这样发送的。尽管内容类型为 application/x-www-form-urlencoded
,但请求正文并未隐藏在服务器端。
解决方法
添加消费后效果很好。
@FeignClient(name = "email",url = "localhost:3000",configuration = EmailClientConfiguration.class)
public interface EmailClient {
@PostMapping(value = "/email/send",consumes = "application/x-www-form-urlencoded")
ResponseDto sendEmail(@RequestBody Map<String,String> requestBody);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。