我已经为 spring open feign 添加了必要的依赖项,如中提到的https://github.com/OpenFeign/feign-form https://github.com/OpenFeign/feign-form并遵循上述 feign-client 的配置。
每当我发送内容类型为 application/x-www-form-urlencoded 的发布请求时。请求正文未正确生成。
电子邮件客户端.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", "[email protected] /cdn-cgi/l/email-protection");
requestBody.put("key", "xxx");
当我在接口中调用 sendEmail 方法时,请求者标头设置正确,但请求正文发送为
{"{\n \"key\" : \"xxx\",\n \"email\" : \"[email protected] /cdn-cgi/l/email-protection\",\n \"username\" : \"xyz\"\n}"
有人可以帮忙解决这个问题吗?为什么请求体是这样发送的。而且请求主体也没有隐藏在服务器端,尽管内容类型是application/x-www-form-urlencoded
.