Spring WebFlux WebClient - How to resolve 400 Bad Request(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 WebFlux WebClient-如何解决400个错误请求
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 转换 ldap 日期 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01