jackson 将嵌套的 hashmap 映射到 java 中的嵌套 pojo class

jackson mapping nested hashmap to nested pojo class in java

我有一个这样的嵌套 java 地图 inputMap: {jobId={EndpointReference={ReferenceParameters={ResourceURI=http://schemas.com/wbem/wscim/1/cim-schema/2/Job, SelectorSet={Selector=[JID_502260561923, root/im]}}, Address=http ://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous}}, returncode=4096, messageId=null, arguments=null, message=null} 我想映射到 java pojo,这是我的 pojo 类.

@Getter
@Setter
@ToString
public class DMResponseMapper {
    @Getter
    @Setter
    @ToString
    public static class GetSysConfigDMResponseMapper {
        @JsonProperty("jobId")
        private EndpointReferenceMapper endpointReferenceMapper;
        private Integer returnCode;
        private String messageId;
        private String arguments;
        private String message;

        @Getter
        @Setter
        @ToString
        public static class EndpointReferenceMapper {
            @JsonProperty("ReferenceParameters")
            private ReferenceParametersMapper referenceParametersMapper;
            @JsonProperty("Address")
            private String address;

                @Getter
                @Setter
                @ToString
                public static class ReferenceParametersMapper {

                    @JsonProperty("ResourceURI")
                    private String resourceURI;

                    @JsonProperty("SelectorSet")
                    private SelectorSetMapper selectorSetMapper;

                    @Getter
                    @Setter
                    @ToString
                    public static class SelectorSetMapper {

                        @JsonProperty("Selector")
                        private List<String> selector;
                    }
                }
            }
        }
    }

但 objectMapper.convertValue(inputMap, GetSysConfigDMResponseMapper.class) 不映射嵌套的 类.. 只是顶级字段。 我的 objectMapper 是这样实例化的:

static {
objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

}

响应对象是:

DMResponseMapper.GetSysConfigDMResponseMapper(endpointReferenceMapper=DMResponseMapper.GetSysConfigDMResponseMapper.EndpointReferenceMapper(referenceParametersMapper=null, address=null), returnCode=4096, messageId=null, arguments=null, message=null)

谁能给点建议,这里有什么问题吗?

调试后,我看到的是: 将 endpointReferenceMapper 转换为 Object 类型。

DMResponseMapper.GetSysConfigDMResponseMapper(endpointReferenceMapper={EndpointReference={ReferenceParameters={ResourceURI=http://schemas.com/wbem/wscim/1/cim-schema/2/Job, SelectorSet={Selector=[JID_502318722705, root/dcim]}}, Address=http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous}}, returnCode=4096, messageId=null, arguments=null, message=null)

DMResponseMapper pojo 需要更紧密地遵循源数据的结构。

根据问题中的信息,您的源 Map 对象具有以下结构:

inputMap: 
{
  jobId={
    EndpointReference={
      ReferenceParameters={
        ResourceURI=http://schemas.com/wbem/wscim/1/cim-schema/2/Job, 
        SelectorSet={
          Selector=[JID_502260561923, root/im]
        }
      }, 
      Address=http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
    }
  }, 
  returncode=4096, 
  messageId=null, 
  arguments=null, 
  message=null
}

因此,我调整了您的 DMResponseMapper pojo class 以更紧密地映射到该结构 - 我还更改了嵌套的 class 名称。以下是嵌套 classes 及其数据字段的摘要:

//
// NOT the actual class - just an overview of the structure!
//
class DMResponseMapper {

    private JobId jobId;
    private Integer returncode;
    private Object messageId;
    private Object arguments;
    private Object message;

    class JobId {

        private EndpointReference endpointReference;

        class EndpointReference {

            private ReferenceParameters referenceParameters;
            private String address;

            class ReferenceParameters {

                private String resourceURI;
                private SelectorSet selectorSet;

                class SelectorSet {
                    private List<String> selector = null;
                }
            }
        }
    }
}

这给了我以下内容,当用注释和 getters/setters 充实时:

//
// Here is the actual class, based on the above structure.
//
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

public class DMResponseMapper {

    @JsonProperty("jobId")
    private JobId jobId;
    @JsonProperty("returncode")
    private Integer returncode;
    @JsonProperty("messageId")
    private Object messageId;
    @JsonProperty("arguments")
    private Object arguments;
    @JsonProperty("message")
    private Object message;

    @JsonProperty("jobId")
    public JobId getJobId() {
        return jobId;
    }

    @JsonProperty("jobId")
    public void setJobId(JobId jobId) {
        this.jobId = jobId;
    }

    @JsonProperty("returncode")
    public Integer getReturncode() {
        return returncode;
    }

    @JsonProperty("returncode")
    public void setReturncode(Integer returncode) {
        this.returncode = returncode;
    }

    @JsonProperty("messageId")
    public Object getMessageId() {
        return messageId;
    }

    @JsonProperty("messageId")
    public void setMessageId(Object messageId) {
        this.messageId = messageId;
    }

    @JsonProperty("arguments")
    public Object getArguments() {
        return arguments;
    }

    @JsonProperty("arguments")
    public void setArguments(Object arguments) {
        this.arguments = arguments;
    }

    @JsonProperty("message")
    public Object getMessage() {
        return message;
    }

    @JsonProperty("message")
    public void setMessage(Object message) {
        this.message = message;
    }

    public static class JobId {

        @JsonProperty("EndpointReference")
        private EndpointReference endpointReference;

        @JsonProperty("EndpointReference")
        public EndpointReference getEndpointReference() {
            return endpointReference;
        }

        @JsonProperty("EndpointReference")
        public void setEndpointReference(EndpointReference endpointReference) {
            this.endpointReference = endpointReference;
        }

        public static class EndpointReference {

            @JsonProperty("ReferenceParameters")
            private ReferenceParameters referenceParameters;
            @JsonProperty("Address")
            private String address;

            @JsonProperty("ReferenceParameters")
            public ReferenceParameters getReferenceParameters() {
                return referenceParameters;
            }

            @JsonProperty("ReferenceParameters")
            public void setReferenceParameters(ReferenceParameters referenceParameters) {
                this.referenceParameters = referenceParameters;
            }

            @JsonProperty("Address")
            public String getAddress() {
                return address;
            }

            @JsonProperty("Address")
            public void setAddress(String address) {
                this.address = address;
            }

            public static class ReferenceParameters {

                @JsonProperty("ResourceURI")
                private String resourceURI;
                @JsonProperty("SelectorSet")
                private SelectorSet selectorSet;

                @JsonProperty("ResourceURI")
                public String getResourceURI() {
                    return resourceURI;
                }

                @JsonProperty("ResourceURI")
                public void setResourceURI(String resourceURI) {
                    this.resourceURI = resourceURI;
                }

                @JsonProperty("SelectorSet")
                public SelectorSet getSelectorSet() {
                    return selectorSet;
                }

                @JsonProperty("SelectorSet")
                public void setSelectorSet(SelectorSet selectorSet) {
                    this.selectorSet = selectorSet;
                }

                public static class SelectorSet {

                    @JsonProperty("Selector")
                    private List<String> selector = null;

                    @JsonProperty("Selector")
                    public List<String> getSelector() {
                        return selector;
                    }

                    @JsonProperty("Selector")
                    public void setSelector(List<String> selector) {
                        this.selector = selector;
                    }

                }

            }

        }

    }

}

调用如下:

首先,一些测试数据:

List<String> selector = new ArrayList();
selector.add("JID_502260561923");
selector.add("root/im");

Map<String, Object> selectorSet = new HashMap();
selectorSet.put("Selector", selector);

String resourceURI = "http://schemas.com/wbem/wscim/1/cim-schema/2/Job";

Map<String, Object> referenceParameters = new HashMap();
referenceParameters.put("ResourceURI", resourceURI);
referenceParameters.put("SelectorSet", selectorSet);

String address = "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous";

Map<String, Object> endpointReference = new HashMap();
endpointReference.put("ReferenceParameters", referenceParameters);
endpointReference.put("Address", address);

Map<String, Object> jobId = new HashMap();
jobId.put("EndpointReference", endpointReference);

Map<String, Object> inputMap = new HashMap();
inputMap.put("jobId", jobId);
inputMap.put("returncode", 4096);
inputMap.put("messageId", "foo");
inputMap.put("arguments", "bar");
inputMap.put("message", "baz");

请注意,为了测试和演示,我用字符串替换了您的 null 值。

然后是执行映射的代码:

ObjectMapper objectMapper = new ObjectMapper();
DMResponseMapper mapper = objectMapper.convertValue(inputMap, DMResponseMapper.class);

生成的 mapper 对象包含测试数据: