I have an API which responds with the following JSON data
{
"message": "...",
"status": "OK",
"data": {
"host": "10.200.100.1",
"port": 8080
}
}
I am trying to request this API and store the result into a ConfigServiceResponse
Java class.
@Data
public class ConfigServiceResponse {
private String message;
private String status;
private SampleHostConfig data;
}
@Data
public class SampleHostConfig {
private String host;
private int port;
}
Here is the code that I am using to make a request
import org.springframework.web.reactive.function.client.WebClient;
WebClient webClient = WebClient.builder().baseUrl(baseUrl).build();
webClient
.get()
.uri("/config?configId=" + configId + "&configVersion=" + configVersion)
.retrieve()
.bodyToMono(ConfigServiceResponse.class)
.block();
I am getting the following error when i am executing the above code
Caused by: org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json' not supported for bodyType=com.w3worker.ConfigServiceResponse
While if i try to deserialise it into String, it’s working fine. Not able to understand, what is causing the issue here.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.w3worker</groupId>
<artifactId>config-service-client</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webflux -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<version>6.1.8</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.30</version>
</dependency>
</dependencies>
</project>