【问题标题】:How to add request attributes to a WebTestClient / WebClient?如何向 WebTestClient / WebClient 添加请求属性?
【发布时间】:2021-12-30 02:25:01
【问题描述】:

我环顾四周,但似乎没有合适的解决方案来解决我并不孤单的问题:为什么我无法向 WebClient(或分别为 WebTestClient)请求添加属性?每当我尝试这样的事情时:

var respSpec = webTestClient.post()
        .uri("http://localhost:10080" + "/endpoint")
        .header(apiSecretHeader, secret)
        .header("HEADER_1", "header value 1")
        .header("HEADER_2", "header value 2")
        .attribute("requestId",  UUID.randomUUID().toString())
        .attribute("aUrl", theUrl)   // private URL theUrl;
  //    .attributes(m())
        .exchange();

我收到错误请求 (400) 作为响应。尝试使用 .attributes 而不是 .attribute 也会导致 400。

REST 控制器的端点:

@PostMapping(value = "/endpoint", produces = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseStatus(HttpStatus.ACCEPTED)
    public ResponseEntity<?> triggerAction(@RequestAttribute final HttpHeaders httpHeaders, @RequestAttribute final String requestId, @NonNull @RequestAttribute(value = "myActionUrl") final URL myActionUrl) {
    ...
}

控制台输出:

2021-11-19 09:06:08.050 ERROR [main] [] [] [VNB_REST_OUTBOUND_ADAPTER] [system] [Initiale Log Konfiguration] o.s.t.w.r.server.ExchangeResult:231 - Request details for assertion failure:

> POST http://localhost:10080/endpoint
> WebTestClient-Request-Id: [1]
> X-SECRET: [secret]
> HEADER1: [header value 1]
> HEADER2: [header value 2]

No content

< 400 BAD_REQUEST Bad Request
< Content-Type: [application/json]
< Transfer-Encoding: [chunked]
< Date: [Fri, 19 Nov 2021 09:06:07 GMT]
< Connection: [close]

{
  "timestamp" : "2021-11-19T09:06:07.963+00:00",
  "status" : 400,
  "error" : "Bad Request",
  "path" : "/endpoint"
}
 

java.lang.AssertionError: Status expected:<204 NO_CONTENT> but was:<400 BAD_REQUEST>
Expected :204 NO_CONTENT
Actual   :400 BAD_REQUEST

您可以看到我尝试添加的属性似乎没有考虑。

【问题讨论】:

    标签: spring-boot attributes bad-request webtestclient


    【解决方案1】:

    假设您正在谈论请求参数,最简单的方法是使用WebClient.Builder,如下所示:

    var uriTemplate = "http://localhost:10080/endpoint?requestId={requestId}&aUrl={aUrl}";
    var respSpec = webClientBuilder.baseUr(uriTemplate).build()
          .post()
          .uri( uri -> uri.build(UUID.randomUUID(), theUrl) )
          .header(apiSecretHeader, secret)
          .header("HEADER_1", "header value 1")
          .header("HEADER_2", "header value 2")
          .exchange();
    

    如果您不想使用 URI 模板,则需要按如下方式指定 URI 中的所有内容:

    var respSpec = webTestClient.post()
          .uri( uriBuilder - > uriBuilder
            .scheme("http")
            .host("localhost")
            .port("10080")
            .path("/endpoint")
            .queryParam("requestId", requestId)
            .queryParam("aUrl", aUrl)
            .build())
          .header(apiSecretHeader, secret)
          .header("HEADER_1", "header value 1")
          .header("HEADER_2", "header value 2")
          .exchange();
    

    【讨论】:

    • 与此同时,我找到了您的解决方案,因为属性用于从服务器端添加属性/值......而不是从客户端添加我打算做的事情。
    • 无论如何...我目前不熟悉 uriTemplate.. 但您提供的代码不起作用。我必须使用: .uri( uriTemplate.expand(UUID.randomUUID().toString(), aUrl) )
    • 谢谢。事实上,我确实搞砸了 URI 模板解决方案。现在应该没问题了。
    猜你喜欢
    • 2019-04-30
    • 2021-06-19
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    相关资源
    最近更新 更多