Getting a null when i make call to post endpoint using spring webclient
I tried using webclient post end point. Got null instead og Object as return type
final int size = 16 * 1024 * 1024;
final ExchangeStrategies strategies = ExchangeStrategies.builder()
.codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size))
.build();
//DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory();
@Bean
public WebClient webClient() {
//factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.URI_COMPONENT);
return WebClient
.builder()
//.uriBuilderFactory(factory)
.exchangeStrategies(strategies)
.build();
}
Object = countryz = webClient.post()
.uri(new URI("https://countriesnow.space/api/v0.1/countries/population"))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
//.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body(BodyInserters.fromObject(country))
//.bodyValue("{\n\t\"country\": \"nigeria\"\n}")
//.syncBody(country)
.retrieve().bodyToMono(Object.class).block();
CodePudding user response:
- Create Weblcient Bean
@Bean
public WebClient webClient() {
final int size = 16 * 1024 * 1024;
final ExchangeStrategies strategies = ExchangeStrategies.builder()
.codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size))
.build();
return WebClient.builder()
.exchangeStrategies(strategies)
.build();
}
- In your service class
@Autowired
private WebClient webClient;
Object countryz = webClient.post()
.uri("https://countriesnow.space/api/v0.1/countries/population")
.header("cache-control", "no-cache")
.header("content-type", "application/json")
.body(BodyInserters.fromObject(Collections.singletonMap("country", "nigeria")))
.retrieve().bodyToMono(Object.class).block();
CodePudding user response:
It looks like you're trying to use the WebClient to send a POST request to a server and retrieve the response as an Object. However, the Object class in Java is a generic class that represents a generic object. It doesn't have any fields or methods of its own, so you won't be able to access any data contained in the response using this class.
To fix this issue, you'll need to use a class that is specific to the type of data you expect to receive in the response. For example, if you expect the response to be a JSON object, you could use the JsonNode class from the Jackson library to represent the response. Here's an example of how you could do this:
final int size = 16 * 1024 * 1024;
final ExchangeStrategies strategies = ExchangeStrategies.builder()
.codecs(codecs -> codecs.defaultCodecs().maxInMemorySize(size))
.build();
@Bean
public WebClient webClient() {
return WebClient
.builder()
.exchangeStrategies(strategies)
.build();
}
JsonNode response = webClient.post()
.uri(new URI("https://countriesnow.space/api/v0.1/countries/population"))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromObject(country))
.retrieve().bodyToMono(JsonNode.class).block();
Alternatively, you could also define a custom class to represent the data in the response, and use that class instead of Object or JsonNode. For example:
public class CountryResponse {
private String name;
private long population;
// other fields and getters/setters
}
CountryResponse response = webClient.post()
.uri(new URI("https://countriesnow.space/api/v0.1/countries/population"))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromObject(country))
.retrieve().bodyToMono(CountryResponse.class).block();
I hope this helps!