簡體   English   中英

從 RestTemplate 遷移到 WebClient 的問題(Spring Boot 2.0.0.M3)

[英]Issues moving from RestTemplate to WebClient (Spring Boot 2.0.0.M3)

被這個問題難住了一段時間!

從常規 MVC 項目轉移到反應式項目,並且正在使用 Spring Boot(新版本 2.0.0.M3)。

在出現這個特定問題之前,我對整個庫的問題都為零。

在使用 WebClient 時,我有一個無效的請求。 以前用 RestTemplate 工作得很好:

rt.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
headers.add("Authorization", "Basic REDACTED");

HttpEntity<OtherApiRequest> entity = 
new HttpEntity<OtherApiRequest>(CrawlRequestBuilder.buildCrawlRequest(req), headers);

ResponseEntity<Void> response = rt.postForEntity("https://other_api/path", 
    entity, 
    Void.class);

System.out.println(response.getStatusCode());

我的 WebClient 代碼:

client
  .post()
  .uri("https://other_api/path")
  .header("Authorization", "Basic REDACTED")
  .contentType(MediaType.APPLICATION_JSON)
  .body(Mono.just(req), OtherApiRequest.class)
  .exchange()
  .then(res -> System.out.println(res.getStatusCode()));

我也試過先生成身體:

ObjectMapper mapper = new ObjectMapper();
String body = mapper.writeValueAsString(
client
  .post()
  .uri("https://other_api/path")
  .header("Authorization", "Basic REDACTED")
  .contentType(MediaType.APPLICATION_JSON)
  .body(body, String.class)
  .exchange()
  .then(res -> System.out.println(res.getStatusCode()));

這里有什么地方是錯誤的嗎? 我看不出兩者之間有任何問題會導致第二個失敗......

編輯:RestTemplate 提供了 204 的響應。WebClient 提供了 400 的響應,表示主體是無效的 JSON。 使用上面 WebClient 的第二個示例,我可以打印body變量並查看它是正確的 JSON。

Edit2:我正在序列化的 POJO 類:

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class OtherApiRequest {
    private String app;
    private String urllist;
    private int maxDepth;
    private int maxUrls;

   public OtherApiRequest(String app, String urllist, int maxDepth, int maxUrls) {
        this.app = app;
        this.urllist = urllist;
        this.maxDepth = maxDepth;
        this.maxUrls = maxUrls;
    }

   public String getApp() {
        return app;
    }

   public String getUrllist() {
        return urllist;
    }

   public int getMaxDepth() {
        return maxDepth;
    }

   public int getMaxUrls() {
        return maxUrls;
    }

   public String toString() {
        return "OtherApiRequest: {" +
            "app: " + app + "," +
            "urllist: " + urllist + "," +
            "max_depth: " + maxDepth + "," +
            "max_urls: " + maxUrls +
            "}";
    }
}

編輯:

在此處查看更好的答案

缺少 Content-Length 標頭使用 WebClient 發送 POST 請求(SpringBoot 2.0.2.RELEASE)

錯誤報告

https://github.com/spring-projects/spring-framework/issues/21085

在 2.2 中修復

當我遇到“無效的 JSON 響應”時,我通過 netcat 查看了 WebClient 請求,發現實際的有效負載,在本例中為3.16包含在某種內容信息中:

$ netcat -l 6500
PUT /value HTTP/1.1
user-agent: ReactorNetty/0.7.5.RELEASE
transfer-encoding: chunked
host: localhost:6500
accept-encoding: gzip
Content-Type: application/json

4
3.16
0

在我將contentLength()添加到構建器后,前面的 4 和后面的 0 消失了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM