在外部客户机中使用spring form编码器时,java请求主体未正确编码和隐藏

u1ehiz5o  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(217)

我为springopen-feign添加了必要的依赖项,如中所述https://github.com/openfeign/feign-form 并遵循前面提到的针对外国客户机的配置。
每当我发送内容类型为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 .

kse8i1jr

kse8i1jr1#

添加消耗后,它工作正常。

@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);

}

相关问题