javaspring-bootspring-webclient

Can not deserialize Spring Boot WebClient response to Java POJO


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>

Solution

  • The Spring Boot starters are the answer to your question. They are designed to provide all the auto configuration and related dependencies.

    Using https://start.spring.io/ you will get the correct pom.xml for a Spring Boot project with Webflux.

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>3.3.0</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.w3worker</groupId>
        <artifactId>config-service-client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>config-service-client</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>22</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-webflux</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>io.projectreactor</groupId>
                <artifactId>reactor-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>