You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询:Spring Boot WebClient.get()无法正确映射JSON至Java对象

解决Spring Boot WebClient JSON映射问题:提取嵌套data数组映射到User对象

问题根源

接口返回的JSON是包含分页元数据、data用户数组和support信息的完整对象,而非直接的User数组。直接调用bodyToFlux(User.class)会尝试将整个外层JSON对象解析为User实例,导致字段不匹配,映射失败。

解决方案

方案1:创建外层响应DTO类(推荐,规范易维护)

定义与接口返回结构完全匹配的DTO类,通过它提取data数组并转换为Flux。

  1. 定义外层响应类UserResponse:
import lombok.Data;
import java.util.List;

@Data
public class UserResponse {
    private int page;
    private int per_page;
    private int total;
    private int total_pages;
    private List<User> data;
    private Support support;

    @Data
    public static class Support {
        private String url;
        private String text;
    }
}

(若未使用Lombok,需手动生成所有字段的getter/setter方法)

  1. 修改WebClient调用代码:
public Flux<User> getUsers() {
    return webClient.get()
            .uri("/api/users?page=1")
            .retrieve()
            .bodyToMono(UserResponse.class)
            .flatMapIterable(UserResponse::getData); // 将List<User>转换为Flux<User>
}

这种方式不仅能获取用户数据,还能方便复用分页元数据(如total_pages、page)。

方案2:直接解析JSON提取data数组(灵活,无需额外DTO)

利用Jackson的JsonNode直接提取data节点,无需创建外层DTO类。

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import org.springframework.web.reactive.function.client.WebClient;

@Component
public class UserClient {
    private final WebClient webClient;
    private final ObjectMapper objectMapper;

    @Autowired
    public UserClient(WebClient webClient, ObjectMapper objectMapper) {
        this.webClient = webClient;
        this.objectMapper = objectMapper;
    }

    public Flux<User> getUsers() {
        return webClient.get()
                .uri("/api/users?page=1")
                .retrieve()
                .bodyToMono(JsonNode.class)
                .map(jsonNode -> jsonNode.get("data"))
                .map(dataNode -> objectMapper.convertValue(dataNode, new TypeReference<List<User>>() {}))
                .flatMapIterable(users -> users);
    }
}

注意事项

确保User类的字段与JSON中的字段正确映射:

  • 对于JSON中的蛇形命名(如first_name),可在User类字段上添加@JsonProperty("first_name")注解;
  • 或在application.properties中全局配置Jackson的命名策略:
spring.jackson.property-naming-strategy=SNAKE_CASE

内容的提问来源于stack exchange,提问作者Uriel Zamora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 01:45:01