0

我一直在努力构建 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");

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

4

1 回答 1

0

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

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

这个客户端通信层是用

  • 弹簧靴 2.6.0
  • 假装形式 3.8.0
  • 假装形式弹簧 3.8.0
  • 伪装核心 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;

}
于 2021-11-27T19:48:41.477 回答