日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了Spring WebFlux WebClient-如何解決400個錯誤請求的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我是反應式編程的新手,我正在使用Spring WebFlux的WebClient向下面的URL發出POST請求,作為我的Spring Boot應用程序的一部分,以便將現有的測驗分配給考生。我無法理解我在構造WebClient請求時做錯了什么。

終點

https://www.flexiquiz.com/api/v1/users/{user_id}/quizzes

在我的請求正文中,我需要傳遞從另一個API獲得的測驗ID(工作正常)。

{
   "quiz_id": ""
}

除了傳遞請求正文外,我還將X-api-key作為請求標頭的一部分傳遞。

但是,當我嘗試到達終結點時,收到{“Message”:”400:Bad Request”}錯誤。

下面是我的代碼。

QuizRequest.java

@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class QuizRequest {

  @JsonProperty("quiz_id")
  @NotBlank
  private String quizId;

  public QuizRequest(@NotBlank String quizId) {
    this.quizId = quizId;
  }
}

FlexiQuizClient.java

@Service
@Slf4j
public class FlexiQuizClient {

  private static final String USER_AGENT = "WebClient for FlexiQuiz";

  private final WebClient webClient;

  @Value("${flexiquiz.baseurl}")
  private String FLEXIQUIZ_API_BASE_URL;

  @Value("${flexiquiz.key}")
  private String FLEXIQUIZ_API_KEY;

  @Autowired
  public FlexiQuizClient() {
    this.webClient = WebClient.builder()
        .baseUrl(FLEXIQUIZ_API_BASE_URL)
        .defaultHeader(HttpHeaders.USER_AGENT, USER_AGENT)
        .filter(logRequest())
        .build();
  }

  public String assignQuizToCandidate(String userId, QuizRequest quizRequest) {
    return Objects.requireNonNull(webClient.post()
        .uri(FLEXIQUIZ_API_BASE_URL + "/v1/users/" + userId + "/quizzes")
        .header("X-API-KEY", FLEXIQUIZ_API_KEY)
        .body(Mono.just(quizRequest), QuizRequest.class)
        .exchange()
        .block())
        .bodyToMono(String.class)
        .block();
  }

  private ExchangeFilterFunction logRequest() {
    return (clientRequest, next) -> {
      log.info("Request: {} {}", clientRequest.method(), clientRequest.url());
      clientRequest.headers()
          .forEach((name, values) -> values.forEach(value -> log.info("{}={}", name, value)));
      return next.exchange(clientRequest);
    };
  }
}

在我的資源類(控制器)中,我調用Web客戶端方法,如下所示:

ResponseResource.java

private String assignQuizToCandidate(String userId, QuizRequest quizRequest)
      throws ParseException {
    log.info("Assigning a quiz based on your skill");
    String message = quizClient.assignQuizToCandidate(userId, quizRequest);
    log.info("message from status: " + message);
    JSONParser parser = new JSONParser();
    JSONObject json = (JSONObject) parser.parse(message);
    return (String) json.get("message");
}

在另一個方法中,我正在調用上面的方法,如下所示。

QuizRequest quizRequest = new QuizRequest(openQuiz.get().getQuizId());
String status = assignQuizToCandidate(userId, quizRequest);

以下是日志:

2020-05-17 10:20:09.938 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Channel acquired, now 1 active connections and 0 inactive connections
2020-05-17 10:20:09.938 DEBUG 32600 --- [ctor-http-nio-1] r.netty.http.client.HttpClientConnect    : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Handler is being applied: {uri=https://www.flexiquiz.com/api/v1/users/{userid}/quizzes, method=POST}
2020-05-17 10:20:09.939 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] onStateChange(POST{uri=/api/v1/users/{userid}/quizzes, connection=PooledConnection{channel=[id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443]}}, [request_prepared])
2020-05-17 10:20:09.939 DEBUG 32600 --- [ctor-http-nio-1] o.s.http.codec.json.Jackson2JsonEncoder  : [1bbedd72] Encoding [QuizRequest(quizId={quizid})]
2020-05-17 10:20:09.941 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] onStateChange(POST{uri=/api/v1/users/{userid}/quizzes, connection=PooledConnection{channel=[id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443]}}, [request_sent])
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.http.client.HttpClientOperations     : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Received response (auto-read:false) : [Cache-Control=private, Content-Type=text/html; charset=utf-8, Server=Microsoft-IIS/10.0, Date=Sun, 17 May 2020 04:50:10 GMT, Content-Length=30]
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] onStateChange(POST{uri=/api/v1/users/{userid}/quizzes, connection=PooledConnection{channel=[id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443]}}, [response_received])
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] o.s.w.r.f.client.ExchangeFunctions       : [1bbedd72] Response 400 BAD_REQUEST
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.http.client.HttpClientOperations     : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Received last HTTP packet
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] onStateChange(POST{uri=/api/v1/users/{userid}/quizzes, connection=PooledConnection{channel=[id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443]}}, [response_completed])
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] onStateChange(POST{uri=/api/v1/users/{userid}/quizzes, connection=PooledConnection{channel=[id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443]}}, [disconnecting])
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Releasing channel
2020-05-17 10:20:10.189 DEBUG 32600 --- [ctor-http-nio-1] r.n.resources.PooledConnectionProvider   : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Channel cleaned, now 0 active connections and 1 inactive connections
2020-05-17 10:20:10.190 DEBUG 32600 --- [ctor-http-nio-1] reactor.netty.channel.FluxReceive        : [id: 0x2b404095, L:/192.168.0.106:62197 - R:www.flexiquiz.com/208.117.41.204:443] Subscribing inbound receiver [pending: 1, cancelled:false, inboundDone: true]
2020-05-17 10:20:10.190 DEBUG 32600 --- [ctor-http-nio-1] o.s.core.codec.StringDecoder             : [1bbedd72] Decoded "{"message":"400: Bad Request"}"
2020-05-17 10:20:10.190  INFO 32600 --- [nio-8086-exec-6] i.d.ivrauto.resource.ResponseResource    : message from status: {"message":"400: Bad Request"}
2020-05-17 10:20:10.190  INFO 32600 --- [nio-8086-exec-6] i.d.ivrauto.resource.ResponseResource    : json.get(message): 400: Bad Request
2020-05-17 10:20:10.190  INFO 32600 --- [nio-8086-exec-6] o.h.e.i.AbstractFlushingEventListener    : Processing flush-time cascades
2020-05-17 10:20:10.195 DEBUG 32600 --- [nio-8086-exec-6] o.h.e.i.AbstractFlushingEventListener    : Dirty checking collections

下面是我嘗試訪問的終結點。

POST: /v1/users/{user_id}/quizzes

請求示例

$ curl https://www.flexiquiz.com/api/v1/users/06e3244f-1381-4da4-aa75-996981b42edb/quizzes 
-H "X-API-KEY: fcb5f59c-2a2f-44a9-8261-33cbfa97be99"
-d quiz_id="1153af46-9580-4672-af78-f23ce2577a14"

回復示例

{                             
    "message": "200: OK"
}

推薦答案

您的問題可能是您發送的數據格式錯誤。您正在以application/json格式在正文中發布數據。

但如果您查看請求,它是使用curl中的-d標志發出的。

來自cURL文檔:

-d,–數據

(HTTP)將POST請求中的指定數據發送到HTTP服務器,其方式與
當用戶填寫了一個HTML表單并按下提交按鈕時,瀏覽器就會執行此操作。這將導致
CURL使用內容類型的應用程序/x-www-form-urlencode將數據傳遞給服務器。
請比較-F,–表單。

這基本上意味著您需要以FORM格式發送數據。

WebFlux文檔介紹如何將數據作為表單請求發送。

Webflux Send FormData

因此您的代碼應該看起來像(某種):

public QuizResponse assignQuizToCandidate(String userId, String quizId) {

    final MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
    formData.add("quiz_id", quizId);

    return webClient.post()
        .uri(FLEXIQUIZ_API_BASE_URL + "/v1/users/" + userId + "/quizzes")
        .header("X-API-KEY", FLEXIQUIZ_API_KEY)
        .bodyValue(formData)
        .retrive()
        .bodyToMono(QuizResponse.class)
        .block();
}

這篇關于Spring WebFlux WebClient-如何解決400個錯誤請求的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標簽:Spring WebClient WebFlux 如何解決 請求 錯誤
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定