通过 WebClient 发送 POST 请求,传递复杂的 JSON 正文的问题

问题描述

希望有人能帮助我。

我在 postman.co 网站上准备了一个带有 JSON 正文的 POST 请求,如下所示:

{
    "user": {
        "name": "John Doe","account": {
            "accountId": "1234","mail": "j.doe@mail.com"
        },"address": null
    },"registrationDate": "2021-07-01","text": null,"description": "something","amount": 800,"currency": "EUR","tex1": null,"text2": null
    "anotherObject": {
        "id": null,"isNew": false,"number": "12345","type": "STUDENT","student": {
            "fiscalCode": "XLFGBT34G34V432W","text2": null
        },"teacher": null
    }

}

关于邮递员,一切都相当不错,回复正确。所以我在我的java代码中复制了它,你如何在下面看到:

AccountDTO account = new AccountDTO("1234","j.doe@mail.com");
UserDTO user = new UserDTO("John Doe",account,null);
StudentDTO student = new StudentDTO("XLFGBT34G34V432W",null,null);
AnotherObjectDTO anotherObject = new AnotherObjectDTO(null,false,"12345","STUDENT",student,null);
RequestDTO request = new RequestDTO(user,"2021-07-01","something",800,"EUR",anotherObject);

最后,我使用以下代码来执行我的 POST 请求:

public String postUserinformationById(String accountId,RequestDTO request) {
    return webClient.post()
            .uri(POST_ACCOUNT_informatION,accountId)
            .header("Auth-Schema",AUTH_SCHEMA)
            .header("apikey",API_KEY)
            .accept(MediaType.APPLICATION_JSON)
            .body(BodyInserters.fromValue(request))
            .retrieve()
            .onStatus(HttpStatus::is4xxClientError,response -> {
                System.out.println("4xx error");
                return Mono.error(new RuntimeException("4xx"));
              })
              .onStatus(HttpStatus::is5xxServerError,response -> {
                System.out.println("5xx error");
                return Mono.error(new RuntimeException("5xx"));
              })
            .bodyToMono(String.class)
            .block();
}

但我收到了“Response 400 BAD_REQUEST”。

Encoding [RequestDTO(user=UserDTO(name=John Doe,account=AccountDTO(accountId=1234 (truncated)...]

有人有什么建议吗,我如何检查我是否发送了正确的请求?

更新: 这里是 GnanaJeyam 要求的错误日志

17:44:38.670 [reactor-http-nio-2] DEBUG 
reactor.netty.transport.TransportConnector - [id:0ca8d08a] Connecting to [sandBox.io/210.278.152.97:443].
17:44:38.712 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id:0ca8d08a,L:/192.168.1.11:63844 - R:sandBox.io/210.278.152.97:443] Registering pool release on close event for channel
17:44:38.713 [reactor-http-nio-2] DEBUG reactor.netty.resources.PooledConnectionProvider - [id:0ca8d08a,L:/192.168.1.11:63844 - R:sandBox.io/210.278.152.97:443] Channel connected,Now: 1 active connections,0 inactive connections and 0 pending acquire requests.
17:44:38.963 [reactor-http-nio-2] DEBUG io.netty.handler.ssl.SslHandler - [id: 0x0ca8d08a,L:/192.168.1.11:63844 - R:sandBox.io/210.278.152.97:443] HANDSHAKEN: protocol:TLSv1.2 cipher suite:TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
17:44:38.964 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id:0ca8d08a,L:/192.168.1.11:63844 - R:sandBox.io/210.278.152.97:443] onStateChange(PooledConnection{channel=[id: 0x0ca8d08a,L:/192.168.1.11:63844 - R:sandBox.io/210.278.152.97:443]},[connected])
17:44:38.973 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id:0ca8d08a-1,L:/192.168.1.11:63844 - R:sandBox.io/210.278.152.97:443] onStateChange(GET{uri=/,connection=PooledConnection{channel=[id: 0x0ca8d08a,L:/192.168.1.11:63844 - R:sandBox.io/210.278.152.97:443]}},[configured])
17:44:38.975 [reactor-http-nio-2] DEBUG reactor.netty.http.client.HttpClientConnect - [id:0ca8d08a-1,L:/192.168.1.11:63844 - R:sandBox.io/210.278.152.97:443] Handler is being applied: {uri=https://sandBox.io/api/v1.0/users/12345/informations,method=POST}
17:44:38.977 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id:0ca8d08a-1,L:/192.168.1.11:63844 - R:sandBox.io/210.278.152.97:443] onStateChange(POST{uri=/api/v1.0/users/12345/informations,[request_prepared])
17:44:39.037 [reactor-http-nio-2] DEBUG org.springframework.http.codec.json.Jackson2JsonEncoder - [4e4efc1b] Encoding [RequestDTO(user=UserDTO(name=John Doe,account=AccountDTO(accountId=1234 (truncated)...]
17:44:39.063 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id:0ca8d08a-1,[request_sent])
17:44:40.180 [reactor-http-nio-2] DEBUG io.netty.handler.codec.compression.brotli - brotli4j not in the classpath; brotli support will be unavailable.
17:44:40.182 [reactor-http-nio-2] DEBUG reactor.netty.http.client.HttpClientOperations - [id:0ca8d08a-1,L:/192.168.1.11:63844 - R:sandBox.io/210.278.152.97:443] Received response (auto-read:false) : [Date=Fri,16 Jul 2021 15:44:40 GMT,Server=Apache,Max-Forwards=19,Via=1.0 fbkprsndgtm01 (),1.0 fbkprsndgtm01 (),X-CorrelationID=Id-67a9f16022bf0ba985c735d6 0; Id-67a9f160b8be591733191a42 0,Strict-Transport-Security=max-age=31536000,Content-Type=application/json,vary=User-Agent,Connection=close,transfer-encoding=chunked]
17:44:40.182 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id:0ca8d08a-1,[response_received])
17:44:40.186 [reactor-http-nio-2] DEBUG org.springframework.web.reactive.function.client.ExchangeFunctions - [4e4efc1b] [0ca8d08a-1,L:/192.168.1.11:63844 - R:sandBox.io/210.278.152.97:443] Response 400 BAD_REQUEST
4xx error
17:44:40.204 [reactor-http-nio-2] DEBUG reactor.netty.channel.FluxReceive - [id:0ca8d08a-1,L:/192.168.1.11:63844 - R:sandBox.io/210.278.152.97:443] FluxReceive{pending=0,cancelled=false,inboundDone=false,inboundError=null}: subscribing inbound receiver
17:44:40.208 [reactor-http-nio-2] DEBUG reactor.netty.http.client.HttpClientOperations - [id:0ca8d08a-1,L:/192.168.1.11:63844 - R:sandBox.io/210.278.152.97:443] Received last HTTP packet
17:44:40.211 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id:0ca8d08a,[response_completed])
17:44:40.213 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id:0ca8d08a,[disconnecting])
17:44:40.219 [reactor-http-nio-2] DEBUG reactor.netty.resources.PooledConnectionProvider - [id:0ca8d08a,L:/192.168.1.11:63844 ! R:sandBox.io/210.278.152.97:443] Channel closed,Now: 0 active connections,0 inactive connections and 0 pending acquire requests.
17:44:40.220 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id:0ca8d08a,L:/192.168.1.11:63844 ! R:sandBox.io/210.278.152.97:443] onStateChange(PooledConnection{channel=[id: 0x0ca8d08a,L:/192.168.1.11:63844 ! R:sandBox.io/210.278.152.97:443]},[disconnecting])

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)