ClientResponse's toEntity method converts it to a ResponseEntity<Mono> object.
So I came up with the following.
public ResponseEntity<String> doGet2(HttpServletRequest request, URI uri) {
ClientResponse clientResponse = webClient.get()
.uri(uri.toASCIIString())
.headers(headers -> headers.putAll(processRequest(request)))
.exchange()
.block();
return clientResponse.toEntity(String.class).block();
}
Answer from Philip John on Stack Overflow Top answer 1 of 3
9
ClientResponse's toEntity method converts it to a ResponseEntity<Mono> object.
So I came up with the following.
public ResponseEntity<String> doGet2(HttpServletRequest request, URI uri) {
ClientResponse clientResponse = webClient.get()
.uri(uri.toASCIIString())
.headers(headers -> headers.putAll(processRequest(request)))
.exchange()
.block();
return clientResponse.toEntity(String.class).block();
}
2 of 3
3
final ResponseEntity<String> response = webClient
.post()
.uri(uriBuilder -> uriBuilder.path("path").build())
.accept(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromFormData(formData))
.retrieve()
.toEntity(String.class)
.block();
Spring
docs.spring.io › spring-framework › docs › current › javadoc-api › org › springframework › web › reactive › function › client › ClientResponse.html
ClientResponse (Spring Framework 7.0.8 API)
Return a builder to mutate this response, for example to change the status, headers, cookies, and replace or transform the body. ... Release the body of this response. ... Return the request associated with the response. ... Return the HTTP status code as an HttpStatusCode value. ... Return the strategies used to convert the body of this response. ... Return this response as a delayed ResponseEntity containing status and headers, but no body.
Spring
docs.spring.io › spring-framework › reference › integration › rest-clients.html
REST Clients :: Spring Framework
There are adapter implementations for RestClient, WebClient, and RestTemplate. Occasionally, it may be useful to intercept client invocations through a decorator configurable in the HttpServiceProxyFactory.Builder. For example, you can apply built-in decorators to suppress 404 exceptions and return a ResponseEntity with NOT_FOUND and a null body:
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
Spring
docs.spring.io › spring-framework › docs › current › javadoc-api › org › springframework › http › class-use › ResponseEntity.html
Uses of Class org.springframework.http.ResponseEntity (Spring Framework 7.0.9 API)
Execute the HTTP method to the given URI template, writing the given request entity to the request, and return the response as ResponseEntity.
Spring
docs.spring.io › spring-framework › docs › 5.1.8.RELEASE_to_5.1.9.RELEASE › Spring Framework 5.1.9.RELEASE › org › springframework › web › reactive › function › client › ClientResponse.html
ClientResponse
Represents an HTTP response, as returned by WebClient and also ExchangeFunction.
Techsphere
techsphere.dev › home › http request with spring webclient
Http Request with Spring WebClient - Techsphere
May 24, 2025 - WebClient can be used for making both synchronous or asynchronous HTTP requests · To send an HTTP GET Request we will have to use .get() method, and for HTTP POST request we will have to use .post() method along with .body() to send the request body · Response bodies can be parsed to specific higher-level types by by providing more specific types. Response headers can be extracted from the response by parsing the response to ResponseEntity
Top answer 1 of 2
1
You can simply use the Reactor's map operator to modify the headers:
return webClient
.get()
.uri("http://localhost:8088/externalService")
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.toEntity(Data[].class)
.map(responseEntity -> responseEntity.getHeaders().add("header", "header-value");
Alternatively, you can use .handle operator in order to provide response processing:
.handle((responseEntity, sink) -> {
if(!isValid(responseEntity)){
sink.error(new InvalidResponseException());
} else if (isOk(responseEntity))
sink.next(responseEntity);
}
else {
//just ignore element
}
})
2 of 2
0
Spring Starter Web dependency was missing in my pom.xml. Found it and added it back. Now able to get WebClient response in ResponseEntity format.
Spring
docs.spring.io › spring-framework › docs › 5.0.0.M5_to_5.0.0.RC1 › Spring Framework 5.0.0.RC1 › org › springframework › web › reactive › function › client › WebClient.ResponseSpec.html
WebClient.ResponseSpec
Extract the body to a Flux. If the response has status code 4xx or 5xx, the Flux will contain a WebClientException. ... Returns the response as a delayed ResponseEntity.
Jhu
jcs.ep.jhu.edu › legacy-ejava-springboot › coursedocs › content › html_single › springmvc-notes.html
Spring MVC
December 3, 2024 - However, it is common to want to have access to more detailed information. For example: ... Failure — was it a 400/BAD_REQUEST, 404/NOT_FOUND, 422/UNPROCESSABLE_ENTITY, or 500/INTERNAL_SERVER_ERROR? Spring can supply that additional information in a ResponseEntity<T>, supplying us with:
HowToDoInJava
howtodoinjava.com › home › spring webflux › spring boot webclient get example
Spring Boot WebClient GET Example
September 19, 2024 - It is part of the Spring WebFlux module and supports synchronous and asynchronous communications with external services. This tutorial discusses the basics of using WebClient in Spring Boot to make GET requests, as well as handling query parameters, headers, cookies, and errors.
Spring
docs.spring.io › spring-framework › reference › web › webflux › controller › ann-methods › responseentity.html
ResponseEntity :: Spring Framework
ResponseEntity is like @ResponseBody but with status and headers. For example: