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

使用RestTemplate提取JSON数据时字段返回Null的问题求助

问题:RestTemplate转换JSON为Employee对象数组时字段全为Null

示例JSON输出

[
    {
        "data": {
            "position": "pos1",
            "firstName": "test",
            "lastName": "doe",
            "employeeId": "123",
            "email": [
                "test@gmail.com"
            ]
        },
        "highlightings": [],
        "sortValues": []
    },
    {
        "data": {
            "position": "pos2",
            "firstName": "test2",
            "lastName": "john",
            "employeeId": "456",
            "email": [
                "test2@gmail.com"
            ]
        },
        "highlightings": [],
        "sortValues": []
    }
]

现有Employee实体类

public class Employee {

    private Long employeeId;
    private String firstName;
    private String lastName;
    private String email;
    private String position;

    // 省略Getter和Setter
}

现有RestTemplate调用代码

ResponseEntity<Employee[]> response = restTemplate.exchange(
            builder.build().encode().toUri(),
            HttpMethod.GET,
            requestEntity,
            Employee[].class
        );

System.out.println(response.getBody());
for(int i = 0; i < response.getBody().length; i++){
    System.out.println(response.getBody()[i].getEmployeeId());
    System.out.println(response.getBody()[i].getEmail());
}

问题原因

  1. JSON结构不匹配:返回的JSON数组中每个元素都包含data、highlightings、sortValues三个节点,而Employee实体直接对应data内部的字段,RestTemplate无法自动穿透外层的data节点完成映射,导致字段全部为null。
  2. 类型不匹配:
    • JSON中的employeeId是字符串类型(如"123"),但实体类中定义为Long类型,转换时会失败;
    • JSON中的email是字符串数组,但实体类中定义为String类型,无法直接映射。

解决方案

方案一:创建外层包装类(推荐)

定义一个对应JSON外层结构的包装类,通过包装类获取内部的Employee对象:

public class EmployeeWrapper {
    private Employee data;
    private List<Object> highlightings;
    private List<Object> sortValues;

    // Getter和Setter方法
    public Employee getData() { return data; }
    public void setData(Employee data) { this.data = data; }
    public List<Object> getHighlightings() { return highlightings; }
    public void setHighlightings(List<Object> highlightings) { this.highlightings = highlightings; }
    public List<Object> getSortValues() { return sortValues; }
    public void setSortValues(List<Object> sortValues) { this.sortValues = sortValues; }
}

修改RestTemplate调用代码,用EmployeeWrapper[]接收响应:

ResponseEntity<EmployeeWrapper[]> response = restTemplate.exchange(
        builder.build().encode().toUri(),
        HttpMethod.GET,
        requestEntity,
        EmployeeWrapper[].class
    );

// 遍历获取Employee对象
for(EmployeeWrapper wrapper : response.getBody()){
    Employee employee = wrapper.getData();
    System.out.println(employee.getEmployeeId());
    System.out.println(employee.getEmail());
}

同时修正Employee实体类的类型问题:

public class Employee {
    // 将employeeId改为String,匹配JSON中的类型;若需Long,可后续手动转换
    private String employeeId;
    private String firstName;
    private String lastName;
    // 将email改为List<String>,匹配JSON中的数组类型
    private List<String> email;
    private String position;

    // 省略Getter和Setter
}

方案二:使用Jackson注解指定嵌套路径

无需创建包装类,通过Jackson的@JsonProperty注解直接指定字段的嵌套路径,同时修正类型:

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

public class Employee {
    @JsonProperty("data.employeeId")
    private String employeeId; // 或Long,需确保JSON字符串可转换为数值
    @JsonProperty("data.firstName")
    private String firstName;
    @JsonProperty("data.lastName")
    private String lastName;
    @JsonProperty("data.email")
    private List<String> email;
    @JsonProperty("data.position")
    private String position;

    // 省略Getter和Setter
}

如果需要将email数组的第一个元素直接映射为String字段,可修改注解:

@JsonProperty("data.email[0]")
private String email;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 08:20:22