Jackson ObjectMapper 不读取内部对象

Jackson ObjectMapper Not Reading Inner Objects

我有一个 JSON 文件

{
    "readServiceAuthorizationResponse": {
        "serviceAuthorization": {
            "serviceAuthorizationId": "50043~220106065198",
            "status": "Approved",
            "receivedDate": "2022-1-6 1:21:12 PM",
            "providerFirstName": "Ranga",
            "providerLastName": "Thalluri",
            "organizationName": "General Hospital",
            "serviceLines": [{
                "statusReason": "Approved",
                "procedureDescription": "Room & board ward general classification",
                "requestedQuantity": "1.00",
                "approvedQuantity": "1.00",
                "deniedQuantity": "",
                "quantityUnitOfMeasure": "Day(s)",
                "providers": [{
                    "providerFirstName": "Ranga",
                    "providerLastName": "Thalluri",
                    "organizationName": ""
                }]
            }]
        }
    }
}

我的 Java 将其读入对象是这样的:

package com.shawn.dto;

import java.nio.file.Files;
import java.nio.file.Paths;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ServiceAuthorizationDTO {
    public String serviceAuthorizationId;
    public String status;
    public String receivedDate;
    public String providerFirstName;
    public String providerLastName;
    public String organizationName;
    public ServiceLine[] serviceLines;
    
    public static ServiceAuthorizationDTO create(String json) {
        ObjectMapper m = new ObjectMapper();
        try {
            Outer outer = m.readValue(json, Outer.class);
            return outer.readServiceAuthorizationResponse.serviceAuthorization;
        } catch (Exception e) {
            return null;
        }
    }
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    static class ReadServiceAuthorizationResponse {
        public ServiceAuthorizationDTO serviceAuthorization;        

    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    static class Outer {
        public ReadServiceAuthorizationResponse readServiceAuthorizationResponse;       
        
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class ServiceLine {
        String statusReason;
        String procedureDescription;
        String requestedQuantity;
        String approvedQuantity;
        String deniedQuantity;
        String quantityUnitOfMeasure;
        Provider[] providers;
        
    }
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Provider {
        String providerFirstName;
        String providerLastName;
        String organizationName;

    }
    
    public static void main(String[] args) {
        try {
            String json = new String(Files.readAllBytes(Paths.get("c:/temp/test.json")));
            ServiceAuthorizationDTO dao = ServiceAuthorizationDTO.create(json);
            System.out.println("serviceAuthorizationId: " + dao.serviceAuthorizationId);
            System.out.println("serviceLines[0].procedureDescription: " + dao.serviceLines[0].procedureDescription);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    
}

当我 运行 我得到这个:

serviceAuthorizationId: 50043~220106065198
serviceLines[0].procedureDescription: null

从JSON读取对象中的外部字段,如providerId。但是 serviceLines 数组显示 1 个元素,并且 class 中的所有字段都是空的。

有什么想法吗?这是我第一次在 JSON 中使用真实对象。我总是将它映射到 Map 对象并手动拉出字段。谢谢

ServiceLineProvider 中的字段具有 package-private 访问修饰符。 Jackson 无法使用其默认设置反序列化为私有字段。因为它需要 getter 或 setter 方法。

解决方案 1:创建字段 public

    public static class ServiceLine {
        public String statusReason;
        public String procedureDescription;
        public String requestedQuantity;
        public String approvedQuantity;
        public String deniedQuantity;
        public String quantityUnitOfMeasure;
        public Provider[] providers;        
    }

方案二:使用@JsonAutoDetect注解

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class ServiceLine {
    String statusReason;
    String procedureDescription;
    String requestedQuantity;
    String approvedQuantity;
    String deniedQuantity;
    String quantityUnitOfMeasure;
    Provider[] providers;
}

解决方案 3:更改 ObjectMapper 的可见性 (doc)

    public static ServiceAuthorizationDTO create(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
            Outer outer = objectMapper.readValue(json, Outer.class);
            return outer.readServiceAuthorizationResponse.serviceAuthorization;
        } catch (Exception e) {
            return null;
        }
    }