将嵌套的哈希映射映射到嵌套的POJO类中(Java中)。

huangapple go评论82阅读模式
英文:

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)
英文:

I have a nested java map like this
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}
which I want to map to java pojo and here is my pojo classes.

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

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

                @Getter
                @Setter
                @ToString
                public static class ReferenceParametersMapper {

                    @JsonProperty(&quot;ResourceURI&quot;)
                    private String resourceURI;

                    @JsonProperty(&quot;SelectorSet&quot;)
                    private SelectorSetMapper selectorSetMapper;

                    @Getter
                    @Setter
                    @ToString
                    public static class SelectorSetMapper {

                        @JsonProperty(&quot;Selector&quot;)
                        private List&lt;String&gt; selector;
                    }
                }
            }
        }
    }

but objectMapper.convertValue(inputMap, GetSysConfigDMResponseMapper.class) is NOT mapping the nested classes.. just the top level fields.
My objectMapper is instantiated like this:

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

}

Response Object is :

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

Can anyone please suggest, what is wrong here?

Upon debugging this is what I see:
Converted endpointReferenceMapper to type 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)

答案1

得分: 1

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 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&lt;String&gt; selector = null;
                }
            }
        }
    }
}

在添加了注解和 getter/setter 后,得到了以下类:

//
// 这是基于上述结构的实际类。
//
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");

然后是执行映射的代码:

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

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

英文:

The DMResponseMapper pojo needs to follow the structure of your source data more closely.

Your source Map object has the following structure, based on the info in the question:

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
}

So, I adapted your DMResponseMapper pojo class to more closely map to that structure - and I changed the nested class names as well. Here is a summary of the nested classes with their fields for your data:

//
// 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&lt;String&gt; selector = null;
}
}
}
}
}

This gave me the following, when fleshed out with annotations and 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(&quot;jobId&quot;)
private JobId jobId;
@JsonProperty(&quot;returncode&quot;)
private Integer returncode;
@JsonProperty(&quot;messageId&quot;)
private Object messageId;
@JsonProperty(&quot;arguments&quot;)
private Object arguments;
@JsonProperty(&quot;message&quot;)
private Object message;
@JsonProperty(&quot;jobId&quot;)
public JobId getJobId() {
return jobId;
}
@JsonProperty(&quot;jobId&quot;)
public void setJobId(JobId jobId) {
this.jobId = jobId;
}
@JsonProperty(&quot;returncode&quot;)
public Integer getReturncode() {
return returncode;
}
@JsonProperty(&quot;returncode&quot;)
public void setReturncode(Integer returncode) {
this.returncode = returncode;
}
@JsonProperty(&quot;messageId&quot;)
public Object getMessageId() {
return messageId;
}
@JsonProperty(&quot;messageId&quot;)
public void setMessageId(Object messageId) {
this.messageId = messageId;
}
@JsonProperty(&quot;arguments&quot;)
public Object getArguments() {
return arguments;
}
@JsonProperty(&quot;arguments&quot;)
public void setArguments(Object arguments) {
this.arguments = arguments;
}
@JsonProperty(&quot;message&quot;)
public Object getMessage() {
return message;
}
@JsonProperty(&quot;message&quot;)
public void setMessage(Object message) {
this.message = message;
}
public static class JobId {
@JsonProperty(&quot;EndpointReference&quot;)
private EndpointReference endpointReference;
@JsonProperty(&quot;EndpointReference&quot;)
public EndpointReference getEndpointReference() {
return endpointReference;
}
@JsonProperty(&quot;EndpointReference&quot;)
public void setEndpointReference(EndpointReference endpointReference) {
this.endpointReference = endpointReference;
}
public static class EndpointReference {
@JsonProperty(&quot;ReferenceParameters&quot;)
private ReferenceParameters referenceParameters;
@JsonProperty(&quot;Address&quot;)
private String address;
@JsonProperty(&quot;ReferenceParameters&quot;)
public ReferenceParameters getReferenceParameters() {
return referenceParameters;
}
@JsonProperty(&quot;ReferenceParameters&quot;)
public void setReferenceParameters(ReferenceParameters referenceParameters) {
this.referenceParameters = referenceParameters;
}
@JsonProperty(&quot;Address&quot;)
public String getAddress() {
return address;
}
@JsonProperty(&quot;Address&quot;)
public void setAddress(String address) {
this.address = address;
}
public static class ReferenceParameters {
@JsonProperty(&quot;ResourceURI&quot;)
private String resourceURI;
@JsonProperty(&quot;SelectorSet&quot;)
private SelectorSet selectorSet;
@JsonProperty(&quot;ResourceURI&quot;)
public String getResourceURI() {
return resourceURI;
}
@JsonProperty(&quot;ResourceURI&quot;)
public void setResourceURI(String resourceURI) {
this.resourceURI = resourceURI;
}
@JsonProperty(&quot;SelectorSet&quot;)
public SelectorSet getSelectorSet() {
return selectorSet;
}
@JsonProperty(&quot;SelectorSet&quot;)
public void setSelectorSet(SelectorSet selectorSet) {
this.selectorSet = selectorSet;
}
public static class SelectorSet {
@JsonProperty(&quot;Selector&quot;)
private List&lt;String&gt; selector = null;
@JsonProperty(&quot;Selector&quot;)
public List&lt;String&gt; getSelector() {
return selector;
}
@JsonProperty(&quot;Selector&quot;)
public void setSelector(List&lt;String&gt; selector) {
this.selector = selector;
}
}
}
}
}
}

This is invoked as follows:

First, some test data:

List&lt;String&gt; selector = new ArrayList();
selector.add(&quot;JID_502260561923&quot;);
selector.add(&quot;root/im&quot;);
Map&lt;String, Object&gt; selectorSet = new HashMap();
selectorSet.put(&quot;Selector&quot;, selector);
String resourceURI = &quot;http://schemas.com/wbem/wscim/1/cim-schema/2/Job&quot;;
Map&lt;String, Object&gt; referenceParameters = new HashMap();
referenceParameters.put(&quot;ResourceURI&quot;, resourceURI);
referenceParameters.put(&quot;SelectorSet&quot;, selectorSet);
String address = &quot;http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous&quot;;
Map&lt;String, Object&gt; endpointReference = new HashMap();
endpointReference.put(&quot;ReferenceParameters&quot;, referenceParameters);
endpointReference.put(&quot;Address&quot;, address);
Map&lt;String, Object&gt; jobId = new HashMap();
jobId.put(&quot;EndpointReference&quot;, endpointReference);
Map&lt;String, Object&gt; inputMap = new HashMap();
inputMap.put(&quot;jobId&quot;, jobId);
inputMap.put(&quot;returncode&quot;, 4096);
inputMap.put(&quot;messageId&quot;, &quot;foo&quot;);
inputMap.put(&quot;arguments&quot;, &quot;bar&quot;);
inputMap.put(&quot;message&quot;, &quot;baz&quot;);

Note I replaced your null values with strings, for testing and demonstration.

Then the code to perform the mapping:

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

The resulting mapper object contains the test data:

将嵌套的哈希映射映射到嵌套的POJO类中(Java中)。

huangapple
  • 本文由 发表于 2020年8月10日 06:15:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63331846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定