Reputation: 5555
I have the following code
public ClientResponse doGet(HttpServletRequest request, URI uri) {
return webClient.get()
.uri(uri.toASCIIString())
.headers(headers -> headers.putAll(processRequest(request))
.exchange()
.block();
}
But when I try to return this ClientResponse through the RestController as follows,
@GetMapping
@ResponseBody
public ClientResponse doGet(HttpServletRequest request) {
ClientResponse node = service.doGet(request);
return node;
}
I get the following error:
org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class org.springframework.web.reactive.function.client.DefaultClientResponse
Basically, the ultimate goal I want is to return the actual response back to the invoker - including the headers, body, cookies etc.
I was thinking of using ResponseEntity like follows.
public ResponseEntity<JsonNode> doGet2(HttpServletRequest request, URI uri) {
Mono<ClientResponse> clientResponse = webClient.get()
.uri(uri.toASCIIString())
.headers(headers -> headers.putAll(processRequest(request))
.exchange();
HttpHeaders headers = clientResponse.map(resp -> resp.headers().asHttpHeaders()).block();
JsonNode body = clientResponse.flatMap(resp -> resp.bodyToMono(JsonNode.class)).block();
ResponseEntity<JsonNode> jsonNode = ResponseEntity.ok().headers(headers).body(body);
return jsonNode;
}
But this is again an overkill. Is there a way to return the response directly using WebClient instead of restructuring the response?
Upvotes: 5
Views: 20107
Reputation: 2243
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();
Upvotes: 3
Reputation: 5555
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();
}
Upvotes: 9
Reputation: 12943
Generally, when you return a pojo as responseBody, spring requires the class to have getters for the fields. Now, since you are not returning a class you own, you can't add them.
Other solution can be to add a library to your project that will convert the pojo into a json. this one can do the trick:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
Upvotes: 1