本文整理了Java中org.springframework.web.reactive.function.client.WebClient.post()
方法的一些代码示例,展示了WebClient.post()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebClient.post()
方法的具体详情如下:
包路径:org.springframework.web.reactive.function.client.WebClient
类名称:WebClient
方法名:post
[英]Start building an HTTP POST request.
[中]开始构建HTTP POST请求。
代码示例来源:origin: spring-projects/spring-framework
@Test(expected = IllegalArgumentException.class)
public void bodyObjectPublisher() {
Mono<Void> mono = Mono.empty();
WebClient client = this.builder.build();
client.post().uri("http://example.com").syncBody(mono);
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void setWebClientCustomThenCustomClientIsUsed() {
WebClient customClient = mock(WebClient.class);
when(customClient.post()).thenReturn(WebClient.builder().build().post());
this.client.setWebClient(customClient);
ClientRegistration registration = this.clientRegistration.build();
enqueueJson("{\n"
+ " \"access_token\":\"MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3\",\n"
+ " \"token_type\":\"bearer\",\n"
+ " \"expires_in\":3600,\n"
+ " \"refresh_token\":\"IwOGYzYTlmM2YxOTQ5MGE3YmNmMDFkNTVk\"\n"
+ "}");
OAuth2ClientCredentialsGrantRequest request = new OAuth2ClientCredentialsGrantRequest(registration);
OAuth2AccessTokenResponse response = this.client.getTokenResponse(request).block();
verify(customClient, atLeastOnce()).post();
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void setCustomWebClientThenCustomWebClientIsUsed() {
WebClient customClient = mock(WebClient.class);
when(customClient.post()).thenReturn(WebClient.builder().build().post());
tokenResponseClient.setWebClient(customClient);
String accessTokenSuccessResponse = "{\n" +
" \"access_token\": \"access-token-1234\",\n" +
" \"token_type\": \"bearer\",\n" +
" \"expires_in\": \"3600\",\n" +
" \"scope\": \"openid profile\"\n" +
"}\n";
this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
this.clientRegistration.scope("openid", "profile", "email", "address");
OAuth2AccessTokenResponse response = this.tokenResponseClient.getTokenResponse(authorizationCodeGrantRequest()).block();
verify(customClient, atLeastOnce()).post();
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-16246
public void shouldSendLargeTextFile() throws IOException {
prepareResponse(response -> {});
Resource resource = new ClassPathResource("largeTextFile.txt", getClass());
byte[] expected = Files.readAllBytes(resource.getFile().toPath());
Flux<DataBuffer> body = DataBufferUtils.read(resource, new DefaultDataBufferFactory(), 4096);
this.webClient.post()
.uri("/")
.body(body, DataBuffer.class)
.retrieve()
.bodyToMono(Void.class)
.block(Duration.ofSeconds(5));
expectRequest(request -> {
ByteArrayOutputStream actual = new ByteArrayOutputStream();
try {
request.getBody().copyTo(actual);
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
assertEquals(expected.length, actual.size());
assertEquals(hash(expected), hash(actual.toByteArray()));
});
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void requestPart() {
Mono<ClientResponse> result = webClient
.post()
.uri("/requestPart")
.syncBody(generateBody())
.exchange();
StepVerifier
.create(result)
.consumeNextWith(response -> assertEquals(HttpStatus.OK, response.statusCode()))
.verifyComplete();
}
代码示例来源:origin: spring-cloud/spring-cloud-gateway
@Test
public void multipartFormDataWorks() {
ClassPathResource img = new ClassPathResource("1x1.png");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
HttpEntity<ClassPathResource> entity = new HttpEntity<>(img, headers);
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
parts.add("imgpart", entity);
Mono<Map> result = webClient.post()
.uri("/post")
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(parts))
.exchange()
.flatMap(response -> response.body(toMono(Map.class)));
StepVerifier.create(result)
.consumeNextWith(map -> {
Map<String, Object> files = getMap(map, "files");
assertThat(files).containsKey("imgpart");
String file = (String) files.get("imgpart");
assertThat(file).startsWith("data:").contains(";base64,");
})
.expectComplete()
.verify(DURATION);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void multipartData() {
Mono<ClientResponse> result = webClient
.post()
.uri("http://localhost:" + this.port + "/multipartData")
.syncBody(generateBody())
.exchange();
StepVerifier
.create(result)
.consumeNextWith(response -> assertEquals(HttpStatus.OK, response.statusCode()))
.verifyComplete();
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void parts() {
Mono<ClientResponse> result = webClient
.post()
.uri("http://localhost:" + this.port + "/parts")
.syncBody(generateBody())
.exchange();
StepVerifier
.create(result)
.consumeNextWith(response -> assertEquals(HttpStatus.OK, response.statusCode()))
.verifyComplete();
}
代码示例来源:origin: spring-projects/spring-security
@Override
public Mono<OAuth2AccessTokenResponse> getTokenResponse(OAuth2AuthorizationCodeGrantRequest authorizationGrantRequest) {
return Mono.defer(() -> {
ClientRegistration clientRegistration = authorizationGrantRequest.getClientRegistration();
OAuth2AuthorizationExchange authorizationExchange = authorizationGrantRequest.getAuthorizationExchange();
String tokenUri = clientRegistration.getProviderDetails().getTokenUri();
BodyInserters.FormInserter<String> body = body(authorizationExchange);
return this.webClient.post()
.uri(tokenUri)
.accept(MediaType.APPLICATION_JSON)
.headers(headers -> headers.setBasicAuth(clientRegistration.getClientId(), clientRegistration.getClientSecret()))
.body(body)
.exchange()
.flatMap(response -> response.body(oauth2AccessTokenResponse()))
.map(response -> {
if (response.getAccessToken().getScopes().isEmpty()) {
response = OAuth2AccessTokenResponse.withResponse(response)
.scopes(authorizationExchange.getAuthorizationRequest().getScopes())
.build();
}
return response;
});
});
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void requestBodyMap() {
Mono<String> result = webClient
.post()
.uri("/requestBodyMap")
.syncBody(generateBody())
.retrieve()
.bodyToMono(String.class);
StepVerifier.create(result)
.consumeNextWith(body -> assertEquals(
"Map[[fieldPart],[fileParts:foo.txt,fileParts:logo.png],[jsonPart]]", body))
.verifyComplete();
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void modelAttribute() {
Mono<String> result = webClient
.post()
.uri("/modelAttribute")
.syncBody(generateBody())
.retrieve()
.bodyToMono(String.class);
StepVerifier.create(result)
.consumeNextWith(body -> assertEquals(
"FormBean[fieldValue,[fileParts:foo.txt,fileParts:logo.png]]", body))
.verifyComplete();
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void requestBodyFlux() {
Mono<String> result = webClient
.post()
.uri("/requestBodyFlux")
.syncBody(generateBody())
.retrieve()
.bodyToMono(String.class);
StepVerifier.create(result)
.consumeNextWith(body -> assertEquals(
"[fieldPart,fileParts:foo.txt,fileParts:logo.png,jsonPart]", body))
.verifyComplete();
}
代码示例来源:origin: spring-projects/spring-security
BodyInserters.FormInserter<String> body = body(authorizationGrantRequest);
return this.webClient.post()
.uri(tokenUri)
.accept(MediaType.APPLICATION_JSON)
代码示例来源:origin: spring-cloud/spring-cloud-gateway
@Test
public void postWorks() {
Mono<Map> result = webClient.post()
.uri("/post")
.header("Host", "www.example.org")
.syncBody("testdata")
.exchange()
.flatMap(response -> response.body(toMono(Map.class)));
StepVerifier.create(result)
.consumeNextWith(map -> assertThat(map).containsEntry("data", "testdata"))
.expectComplete()
.verify(DURATION);
}
代码示例来源:origin: line/armeria
@Test
public void postPerson() {
final Mono<Person> body =
webClient.post()
.uri(uri("/birthday"))
.contentType(MediaType.APPLICATION_JSON)
.body(Mono.just(new Person("armeria", 4)), Person.class)
.retrieve()
.bodyToMono(Person.class);
StepVerifier.create(body)
.expectNext(new Person("armeria", 5))
.expectComplete()
.verify(Duration.ofSeconds(10));
}
代码示例来源:origin: spring-projects/spring-security
WebClient.RequestHeadersSpec<?> requestHeadersSpec;
if (AuthenticationMethod.FORM.equals(authenticationMethod)) {
requestHeadersSpec = this.webClient.post()
.uri(userInfoUri)
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
代码示例来源:origin: spring-projects/spring-framework
@Test
public void shouldSendPojoAsJson() {
prepareResponse(response -> response.setHeader("Content-Type", "application/json")
.setBody("{\"bar\":\"BARBAR\",\"foo\":\"FOOFOO\"}"));
Mono<Pojo> result = this.webClient.post()
.uri("/pojo/capitalize")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.syncBody(new Pojo("foofoo", "barbar"))
.retrieve()
.bodyToMono(Pojo.class);
StepVerifier.create(result)
.consumeNextWith(p -> assertEquals("BARBAR", p.getBar()))
.expectComplete()
.verify(Duration.ofSeconds(3));
expectRequestCount(1);
expectRequest(request -> {
assertEquals("/pojo/capitalize", request.getPath());
assertEquals("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}", request.getBody().readUtf8());
assertEquals("31", request.getHeader(HttpHeaders.CONTENT_LENGTH));
assertEquals("application/json", request.getHeader(HttpHeaders.ACCEPT));
assertEquals("application/json", request.getHeader(HttpHeaders.CONTENT_TYPE));
});
}
代码示例来源:origin: Dromara/soul
private Mono<Void> doHttpInvoke() {
if (requestDTO.getHttpMethod().equals(HttpMethodEnum.GET.getName())) {
String uri = buildRealURL();
if (StringUtils.isNoneBlank(requestDTO.getExtInfo())) {
uri = uri + "?" + GSONUtils.getInstance().toGetParam(requestDTO.getExtInfo());
}
return WEB_CLIENT.get().uri(uri)
.headers(httpHeaders -> {
httpHeaders.addAll(exchange.getRequest().getHeaders());
httpHeaders.remove(HttpHeaders.HOST);
})
.exchange()
.doOnError(e -> LogUtils.error(LOGGER, e::getMessage))
.timeout(Duration.ofMillis(timeout))
.flatMap(this::doNext);
} else if (requestDTO.getHttpMethod().equals(HttpMethodEnum.POST.getName())) {
return WEB_CLIENT.post().uri(buildRealURL())
.headers(httpHeaders -> {
httpHeaders.addAll(exchange.getRequest().getHeaders());
httpHeaders.remove(HttpHeaders.HOST);
})
.contentType(buildMediaType())
.body(BodyInserters.fromDataBuffers(exchange.getRequest().getBody()))
.exchange()
.doOnError(e -> LogUtils.error(LOGGER, e::getMessage))
.timeout(Duration.ofMillis(timeout))
.flatMap(this::doNext);
}
return Mono.empty();
}
代码示例来源:origin: rstoyanchev/demo-reactive-spring
private Mono<ResponseEntity<Void>> requestCar(Car car) {
return bookClient.post()
.uri("/cars/{id}/booking", car.getId())
.exchange()
.flatMap(response -> response.toEntity(Void.class));
}
代码示例来源:origin: spring-cloud/spring-cloud-function
private Mono<ClientResponse> post(URI uri, String destination, Object value) {
Mono<ClientResponse> result = client.post().uri(uri)
.headers(headers -> headers(headers, destination, value))
.body(BodyInserters.fromObject(value)).exchange();
if (this.debug) {
result = result.log();
}
return result;
}
内容来源于网络,如有侵权,请联系作者删除!