Java 用Webflux实现POST
我有一个html表单,当我在web浏览器中打开它时,它工作正常:Java 用Webflux实现POST,java,spring,spring-boot,spring-webflux,Java,Spring,Spring Boot,Spring Webflux,我有一个html表单,当我在web浏览器中打开它时,它工作正常: <html> <head> <meta HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8" /> <meta HTTP-EQUIV="Cache-Control" CONTENT="no cache" /> <meta HTTP-EQUIV="Pragma" CONT
<html>
<head>
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8" />
<meta HTTP-EQUIV="Cache-Control" CONTENT="no cache" />
<meta HTTP-EQUIV="Pragma" CONTENT="no cache" />
<meta HTTP-EQUIV="Expires" CONTENT="0" />
</head>
<body OnLoad="AutoSubmitForm();">
<form name="downloadForm" action="https://c3-test.wirecard.com/acssim/app/bank" method="POST">
<input type="hidden" name="PaReq" value="eJxtU9tuozAQ......." />
<input type="hidden" name="TermUrl" value="https://www.test.com" />
<input type="hidden" name="MD" value="optionalValue" />
<SCRIPT LANGUAGE="Javascript">
function AutoSubmitForm() { document.downloadForm.submit();}
</SCRIPT>
<input type="submit" name="continue" value="Continue" />
</form>
</body>
</html>
但我得到了错误:org.springframework.web.reactive.function.client.WebClientResponseException$BadRequest:400错误请求
实现这一点的正确方法是什么?您可以试试这一种
public static <T> T executeApiForFormData(String url, Map<String, String> payload, Map<String, String> headers, Class<T> clazz) {
WebClient client;
WebClient.Builder builder = WebClient.builder().baseUrl(url);
MultiValueMap<String, String> formData = new HttpHeaders();
formData.setAll(payload);
if (!ObjectUtils.isEmpty(headers)) {
MultiValueMap<String, String> map = new HttpHeaders();
map.setAll(headers);
builder.defaultHeaders((existingHeaders) -> existingHeaders.addAll(map));
}
client = builder.build();
return client
.method(HttpMethod.POST)
.body(BodyInserters.fromFormData(formData))
.retrieve()
.bodyToMono(clazz)
.block();
}
public static T executeAppiforFormData(字符串url、映射负载、映射头、类clazz){
网络客户端;
WebClient.Builder=WebClient.Builder().baseUrl(url);
多值映射formData=新的HttpHeaders();
formData.setAll(有效载荷);
如果(!ObjectUtils.isEmpty(头)){
多值映射=新的HttpHeaders();
map.setAll(标题);
builder.defaultHeaders((existingHeaders)->existingHeaders.addAll(map));
}
client=builder.build();
返回客户端
.方法(HttpMethod.POST)
.body(BodyInserters.fromFormData(formData))
.retrieve()
.bodyToMono(clazz)
.block();
}
通过使用formdata发送formdata,而不是将formdata作为标头发送。。。。
public static <T> T executeApiForFormData(String url, Map<String, String> payload, Map<String, String> headers, Class<T> clazz) {
WebClient client;
WebClient.Builder builder = WebClient.builder().baseUrl(url);
MultiValueMap<String, String> formData = new HttpHeaders();
formData.setAll(payload);
if (!ObjectUtils.isEmpty(headers)) {
MultiValueMap<String, String> map = new HttpHeaders();
map.setAll(headers);
builder.defaultHeaders((existingHeaders) -> existingHeaders.addAll(map));
}
client = builder.build();
return client
.method(HttpMethod.POST)
.body(BodyInserters.fromFormData(formData))
.retrieve()
.bodyToMono(clazz)
.block();
}