webflux构造了不正确的url值

4uqofj5v  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(288)

我想使用springwebflux发出post请求。我试过这个:
web客户端版本:

public RestClient(String gatewayUrl, String token, String username, String password, SslContext sslContext) {
        this.token = token;
        this.gatewayUrl = gatewayUrl;
        WebClient.Builder builder = WebClient.builder().baseUrl(gatewayUrl);
        if (sslContext != null) {
            HttpClient httpClient = HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
            ClientHttpConnector httpConnector = new ReactorClientHttpConnector(httpClient);
            builder.clientConnector(httpConnector);
        }
        if (username != null && password != null) {
            builder.filter(basicAuthentication(username, password));
        }
        client = builder.build();
    }

......

public class RestClientBuilder {
    private String token;
    private String username;
    private String password;
    private URL gatewayUrl;
    private SslContextBuilder sslContextBuilder;

    public static RestClientBuilder builder() {
        return new RestClientBuilder();
    }

    public RestClientBuilder token(String token) {
        this.token = validateAndTrim(token, "Token");
        return this;
    }

    public RestClientBuilder usernamePassword(String username, String password) {
        this.username = validateAndTrim(username, "Username");
        this.password = validateAndTrim(password, "Password");
        return this;
    }

    private String validateAndTrim(String value, final String parameter) {
        if (value == null || value.trim().isEmpty()) {
            throw new IllegalArgumentException(parameter + " is empty");
        }
        return value.trim();
    }

    public RestClientBuilder gatewayUrl(String gatewayUrl) {
        String urlSt = validateAndTrim(gatewayUrl, "Gateway URL");
        try {
            this.gatewayUrl = new URL(urlSt);
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Malformed URL: " + urlSt, e);
        }
        return this;
    }

    public RestClientBuilder truststore(File truststoreFile) {
        getSslContextBuilder().trustManager(truststoreFile);
        return this;
    }

    public RestClientBuilder sslCertificate(File keyCertChainFile, File keyFile, String keyPassword) {
        getSslContextBuilder().keyManager(keyCertChainFile, keyFile, keyPassword);
        return this;
    }

    public RestClient build() throws SSLException {
        SslContext sslContext = sslContextBuilder != null ? sslContextBuilder.build() : null;
        return new RestClient(gatewayUrl.toString(), token, username, password, sslContext);
    }

    private SslContextBuilder getSslContextBuilder() {
        if (sslContextBuilder == null) {
            sslContextBuilder = SslContextBuilder.forClient();
        }
        return sslContextBuilder;
    }

}

测试成功:

client.post()
            .uri("https://test.oppwa.com/v1/checkouts?entityId=1234567&amount=92.00&currency=GBP&paymentType=DB")
            .header(HttpHeaders.USER_AGENT, "Mozilla/5.0")
            .header(HttpHeaders.AUTHORIZATION, "Bearer 11111111")
            .accept(MediaType.APPLICATION_JSON)
            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
            .retrieve()
            .bodyToMono(SaleResponse.class);

失败的请求:

MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("entityId", "1234567");
        map.add("amount", "92.00");
        map.add("currency", "GBP");
        map.add("paymentType", "DB");

            client.post()
                .uri(uriBuilder -> uriBuilder.path("https://test.oppwa.com/v1/checkouts")
                        .queryParams(map)
                        .build())
                .header(HttpHeaders.USER_AGENT, "Mozilla/5.0")
                .header(HttpHeaders.AUTHORIZATION, "Bearer 11111111")
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                .retrieve()
                .bodyToMono(SaleResponse.class);

出于某种原因,webflux构造此值:

.WebClientResponseException$NotFound: 404 Not Found from POST https://test.oppwa.com/v1/checkoutshttps:/test.oppwa.com/v1/checkouts?entityId=1234567&amount=92.00&currency=GBP&paymentType=DB

你知道为什么url链接会重复两次吗?我怎样才能解决这个问题?

wljmcqd8

wljmcqd81#

您正在使用网关基url构建restclient:

WebClient.Builder builder = WebClient.builder().baseUrl(gatewayUrl);

在spring文档中,您可以阅读以下内容 baseUrl :
defaulturibuilderfactory用于为具有给定基url的每个请求准备url,除非给定url的url请求是绝对的,在这种情况下,基url将被忽略。
webclient.builder#基本URL
在您的示例中:

// Here you are supplying a an absolute url, which means base url is ignored
client.post()
      .uri("https://test.oppwa.com/v1/checkouts?entityId=1234567&amount=92.00&currency=GBP&paymentType=DB"

而在你的另一个例子中:

client.post()
      // Here you are supplying only a path
      .uri(uriBuilder -> uriBuilder.path("https://test.oppwa.com/v1/checkouts")
                            .queryParams(map)
                            .build())

对于路径的文档,它说:
附加到此生成器的路径。给定的值按原样追加,除了清除重复项之外,不检查斜杠。
如果您需要更改基本url,我建议您将url放在属性文件中,并在启动时加载它们。因此,根据env(dev,test,prod)的不同,您可以用自己的配置加载不同的概要文件,并将其设置为baseurl。然后使用urlbuilder将不同的路径附加到顶部。
uribuilder路径

相关问题