【问题标题】:Request body is missing in the server end for feign application/x-www-form-urlencoded type requestfeign application/x-www-form-urlencoded 类型请求的服务器端缺少请求体
【发布时间】:2021-06-19 16:36:55
【问题描述】:

我一直致力于构建 feign 客户端以发送表单 urlencoded 请求。问题是它直到昨天都运行良好,没有任何问题。但是现在请求正文没有被发送到服务器。

这是我的配置。


    EmailClientConfiguration.class
    
    public class EmailClientConfiguration  {
        
        @Bean
        public RequestInterceptor requestInterceptor() {
            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());
        }
    }

客户:


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

请求正文:

Map<String, String> requestBody = 
Map.of("username", "xyz",
"email", "xyz@gmail.com",
"key", "xxx");

我从服务器端调试也找不到为什么没有收到请求正文。发送请求时我看不到错误。如何在发送请求之前检查请求正文是否存在。

【问题讨论】:

  • 您正在使用 jackson 编码器和解码器,它可能会将您的参数转换为 json,因此参数可能无法作为表单数据到达,请尝试评论编码器/解码器。

标签: java spring spring-boot spring-cloud-feign openfeign


【解决方案1】:

在使用 Pinterest REST API 中基于 application/x-www-form-urlencoded 的 API 时,我遇到了与 Feign Client 相同的问题。

最后,所有的东西都在下面的设置中工作了,在这里我为 Feign 客户端添加了基本身份验证。

这个客户端通信层是用

开发的
  • 弹簧靴 2.6.0
  • 假装形式 3.8.0
  • feign-form-spring 3.8.0
  • feign-core 11.7
package com.javatodev.app.openfeignposttest.rest.client;

import feign.auth.BasicAuthRequestInterceptor;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(name = "pinterest-api-request-client", url = "https://api.pinterest.com/v5", configuration = PinterestAPIClient.Configuration.class)
public interface PinterestApiRequestClient {

    @PostMapping(path = "/oauth/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    PinterestAccessTokenResponse getAuthenticationToken(@RequestBody PinterestAccessTokenRequest request);

    class Configuration {

@Bean
        public Encoder feignFormEncoder () {
            return new SpringFormEncoder();
        }

        @Bean
        public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
            return new BasicAuthRequestInterceptor("app_id", "app_secret");
        }
    }
}

请求和响应 DTO

package com.javatodev.app.openfeignposttest.rest.client;

import lombok.*;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class PinterestAccessTokenRequest {
    private String code;
    private String redirect_uri;
    private String grant_type;
}
package com.javatodev.app.openfeignposttest.rest.client;

import lombok.*;

@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class PinterestAccessTokenResponse {

    private String access_token;
    private String refresh_token;
    private String response_type;
    private Long expires_in;
    private Long refresh_token_expires_in;
    private String scope;

}

【讨论】:

    猜你喜欢
    • 2020-02-02
    • 2014-03-30
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    相关资源
    最近更新 更多